Popup在一个浏览器上工作但不在另一个上面

时间:2015-05-06 11:20:31

标签: javascript popupwindow

为什么弹出窗口会在一个浏览器上运行(适用于firefox,firefox开发人员版本)而不是另一个浏览器(不在Internet Explorer上,部分在Chrome上)?在某些情况下,弹出窗口可以在浏览器上的某个网站页面上工作,但是在网站上的另一个页面上,相同的弹出窗口将不起作用。弹出窗口位于页脚

<a href="#" onclick="PopupCenter('/terms_conditions.aspx','','550','700')">Terms
                        &amp; Conditions</a>

任何想法?

编辑弹出功能:

<div class="footerTwoBox">
                    <ul class="footTwoLinks" style="width: 500px; float: left;">
                        <li><a href="/voucher">Your Voucher</a></li>
                        <li><a href="#" onclick="PopupCenter('/privacy_policy.aspx','','550','700')">Privacy
                        Policy</a></li>
                        <li><a href="<%=rootUrl %>/customer-care">Contact Us</a></li>
                        <li><a href="/press-room">Press Room</a></li>
                        <li><a href="#" onclick="PopupCenter('/terms_conditions.aspx','','550','700')">Terms
                        & Conditions</a></li>
                        <li><a href="/partners">Partners</a></li>
                        <li><a href="/blog">Blog</a></li>
                        <li><a href="/careers">Careers</a></li>
                        <li><a href="/customer-care/faq" target="_blank">FAQs</a></li>

                    </ul>

当您点击弹出窗口时,它会将您带到屏幕顶部 - 没有弹出窗口。如果我去firefox - 我会在屏幕中间弹出一个弹出窗口...它必须是某种严重的问题,但我无法识别它。

弹出功能

function PopupCenter(pageURL, title, w, h) {
            //alert(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=no, copyhistory=no, width=' + w + ', height=' + h + ', top=' + top + ', left=' + left);
        }

1 个答案:

答案 0 :(得分:1)

最好在false处理程序中返回onclick。链接通常会指向您希望它显示的页面,而弹出脚本则在事件处理程序中调用。当您在函数中返回false时,会阻止浏览器关注该链接。

这可能会也可能不会影响浏览器的行为,具体取决于您使用的软件。这个主题here的写作非常好,涵盖了大部分陷阱。

请尝试以下代码:

function PopupCenter(pageURL, title, w, h) {
  left = (screen.width / 2) - (w / 2);
  top = (screen.height / 2) - (h / 2);
  targetWin = window.open(pageURL, title, 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=no, copyhistory=no, width=' + w + ', height=' + h + ', top=' + top + ', left=' + left);

  if (window.focus) {
    targetWin.focus();
  }

  return false;
}

然后你可以像这样调用它(注意额外的return关键字):

<a href="#" onclick="return PopupCenter('terms_conditions.aspx', '', '550', '700')">Terms &amp; Conditions</a>

完整演示:

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

<head>
  <title>Popup Demo</title>
  <meta http-equiv="content-type" content="text/html;charset=utf-8" />

  <script type="text/javascript">
    <!--
    function PopupCenter(pageURL, title, w, h) {
      left = (screen.width / 2) - (w / 2);
      top = (screen.height / 2) - (h / 2);
      targetWin = window.open(pageURL, title, 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=no, copyhistory=no, width=' + w + ', height=' + h + ', top=' + top + ', left=' + left);

      if (window.focus) {
        targetWin.focus();
      }

      return false;
    }
    // -->
  </script>
</head>

<body>
  <div>
    <a href="#" onclick="return PopupCenter('terms_conditions.aspx', '', '550', '700')">Terms &amp; Conditions</a>
  </div>
</body>

</html>