我正在寻找一种解决方案,它允许我组合两个脚本并让它们以跨浏览器友好的方式一起工作,并符合 XHTML 1.0 过渡标准。
第一个脚本是基于下拉选择的打开文件/位置。
这是脚本:
<script type="text/javascript">
//<![CDATA[
<!--
function go(){
if (document.fooform.fooselect.options[document.fooform.fooselect.selectedIndex].value != "none") {
location = document.fooform.fooselect.options[document.fooform.fooselect.selectedIndex].value
}
}
//-->
//]]>
</script>
这是标记:
<form name="fooform">
<select name="fooselect">
<option selected="selected" value="#">Select a file</option>
<option value="pdfs/somefile1.pdf">file 1</option>
<option value="pdfs/somefile2.pdf">file 2</option>
<option value="pdfs/somefile3.pdf">file 3</option>
<option value="pdfs/somefile4.pdf">file 4</option>
</select>
<input onclick="go()" type="button" value="Get PDF" />
</form>
很直接...
正如标题所述,第二个脚本当然是打开一个居中弹出窗口的脚本。
这是脚本:
<script type="text/javascript">
//<![CDATA[
<!--
function PopupCenter(pageURL, title,w,h) {
var left = (screen.width/2)-(w/2);
var top = (screen.height/2)-(h/2);
var targetWin = window.open (pageURL, title, 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, copyhistory=no, width='+w+', height='+h+', top='+top+', left='+left);
}
//-->
//]]>
</script>
这是标记:
<input onclick="PopupCenter('http://www.stackoverflow.com', 'popfoo',400,400);" type="button" value="Open Center Popup" />
所以我的挑战是创建一个脚本,当用户单击“获取 PDF”按钮时,会打开一个包含 PDF 文件的居中弹出窗口,并且该 PDF 文件将根据他们从下拉列表中选择的选项而改变。
我将在页面上有多个下拉菜单,因此脚本很可能会从document.fooform.fooselect.options
变为document.form1.select1.options
,document.form2.select2.options
等...并且功能将类似于go1()
,go2()
等...
非常感谢任何帮助/提示/建议!
更新2012 年 11 月 22 日星期四
我考虑过牺牲居中弹出功能,而不是让这个脚本在新窗口中打开 PDF,到目前为止,我已经尝试过:
<input type="button" value="Get PDF" onclick="window.open(go().href); return false;" />
也
<input type="button" value="Get PDF" onclick="window.open(go(), 'USER GUIDE', 'width=500,height=300');" />
但是这两个输入都只是在同一个窗口中打开 PDF,这就是我想要避免的。
如果我只是尝试像这样合并上面的两个脚本:
<script type="text/javascript">
//<![CDATA[
<!--
function PopupCenter(pageURL, title, w, h) {
var left = (screen.width / 2) - (w / 2);
var top = (screen.height / 2) - (h / 2);
var targetWin = window.open(pageURL, title, 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, copyhistory=no, width=' + w + ', height=' + h + ', top=' + top + ', left=' + left);
}
var go = function go() {
if (document.fooform.fooselect.options[document.fooform.fooselect.selectedIndex].value != "none") {
location = document.fooform.fooselect.options[document.fooform.fooselect.selectedIndex].value
}
}
//-->
//]]>
</script>
然后使用以下输入触发器:
<input onclick="PopupCenter('go()', 'popfoo',400,400);" type="button" value="Get PDF" />
或者
<input onclick="PopupCenter('go', 'popfoo',400,400);" type="button" value="Get PDF" />
我收到错误消息:
Windows 互联网浏览器
找不到'file:///W:/test/go()'。确保路径或 Internet 地址正确。
[好的]
或者
Windows 互联网浏览器
找不到'file:///W:/test/go'。确保路径或 Internet 地址正确。
[好的]
如果我尝试在输入中不使用单引号,就像这样
<input onclick="PopupCenter(go(), 'popfoo',400,400);" type="button" value="Get PDF" />
我收到错误消息:
Windows 互联网浏览器
找不到'file:///W:/test/undefined'。确保路径或 Internet 地址正确。
[好的]
然后在错误消息上单击“确定”后,PDF 在同一窗口中打开。
请帮忙!!提前谢谢了 :)
更新 2 2012 年 11 月 22 日星期四
在这个小提琴中让它在 IE 中工作(不幸的是)
然后基本上更改location = document.fooform.fooselect.options[document.fooform.fooselect.selectedIndex].value
并声明location
为变量(我认为这不是一个好主意,因为location
它是一个全局变量 - 如果有人可以对此有所了解,那就太好了)然后添加了该行window.open(location)
想让它跨浏览器友好,并希望在新窗口调用方面得到一些帮助(仍然希望将其居中,最好从新窗口中删除一些不需要的东西,如工具栏、状态栏等)
提前致谢!