我在网站的多个页面上都有一个javascript弹出窗口。我使用了在Center a popup window on screen?上找到的居中弹出窗口

function popupwindow(url, title, w, h) {
    var left = (screen.width/2)-(w/2);
    var top = (screen.height/2)-(h/2);
    return window.open(url, title, 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width='+w+', height='+h+', top='+top+', left='+left);
}


此弹出窗口在某些页面上有效,但在其他页面上无效...

我制作了2页要显示的内容,在第一页上显示弹出窗口
http://www.weddingpages.nl/test1.php
在第二页上,弹出窗口不起作用:
http://www.weddingpages.nl/test2.php

页面的来源完全相同(复制,粘贴),仅在第二页上我删除了表单。

因此,如果我理解正确,那么此弹出式JavaScript仅在页面上还有表单时才有效吗?

这是弹出窗口所在页面的代码:

<!DOCTYPE html>
<html>
    <head>
        <title>test1</title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

        <script type="text/javascript">
            function popupwindow(url, title, w, h) {
                var left = (screen.width/2)-(w/2);
                var top = (screen.height/2)-(h/2);
                return window.open(url, title, 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width='+w+', height='+h+', top='+top+', left='+left);
            }
        </script>
    </head>
    <body>
        <form class="form-horizontal" action="" method="post">
            <div class="control-group">
                <label class="control-label" for="title">Naam website / bedrijf:</label>
                <div class="controls">
                    <input type="text" class="span3" id="title" name="title" placeholder="Naam website / bedrijf">
                </div>
            </div>
            <div class="control-group">
                <div class="controls">
                    <button type="submit" name="submit" class="btn">Gegevens opslaan</button>
                </div>
            </div>
        </form>
        <a href="javascript:popupwindow('http://www.google.com/', title, 1400, 700)">centered Pop up window</a>
    </body>
</html>


因此,如果您删除表格,则javascript将停止工作。

有谁能告诉我为什么?我已经尝试了将近2天的解决方案:-(

最佳答案

在将其传递给函数之前,您尚未定义“标题”参数。
尝试更改此:

<a href="javascript:popupwindow('http://www.google.com/', title, 1400, 700)">centered Pop up window</a>


有了这个

<a href="javascript:popupwindow('http://www.google.com/', 'real title', 1400, 700)">centered Pop up window</a>

09-05 18:49
查看更多