0

我正在寻找一种解决方案,它允许我组合两个脚本并让它们以跨浏览器友好的方式一起工作,并符合 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)

想让它跨浏览器友好,并希望在新窗口调用方面得到一些帮助(仍然希望将其居中,最好从新窗口中删除一些不需要的东西,如工具栏、状态栏等)

提前致谢!

4

1 回答 1

0

不会使弹出窗口居中,但会(主要)做我需要做的事情。见 jsfiddle http://jsfiddle.net/77buX/

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="description" content="" />
    <meta name="keywords" content="" />
    <title>test</title>
    </head>
    <body>
        <script type="text/javascript">
            var UG1 = function getUG1() {
                if (document.form1.select1.options[document.form1.select1.selectedIndex].value != "none") {
                    var location = document.form1.select1.options[document.form1.select1.selectedIndex].value;
                    window.open(location)
                }
            }

            var UG2 = function getUG2() {
                if (document.form2.select2.options[document.form2.select2.selectedIndex].value != "none") {
                    var location = document.form2.select2.options[document.form2.select2.selectedIndex].value;
                    window.open(location)
                }
            }
        </script>
        <form action="" name="form1">
            <p class="clear">
                <select name="select1" style="width: 10.5em">
                    <option selected="selected" value="#">Select a user guide</option>
                    <option value="http://manuals.info.apple.com/en_US/ipad_user_guide.pdf">ipad user guide</option>
                    <option value="http://manuals.info.apple.com/en_US/iphone_user_guide.pdf">iphone user guide</option>
                </select>
                <input onclick="getUG1()" type="button" value="Get PDF" />
            </p>
        </form>
        <br />
        <br />
        <form action="" name="form2">
            <p class="clear">
                <select name="select2" style="width: 10.5em">
                    <option selected="selected" value="#">Select a user guide</option>
                    <option value="http://docs.blackberry.com/en/smartphone_users/deliverables/1202/userguide_bb8330_cdma.pdf">blackberry 8330 user guide</option>
                    <option value="http://cb.vu/unixtoolbox.pdf">unix toolbox</option>
                </select>
                <input onclick="getUG2()" type="button" value="Get PDF" />
            </p>
        </form>
    </body>

</html>​
于 2012-12-03T17:57:33.637 回答