javascript - 弹出窗口在一个浏览器上工作,但在另一个浏览器上不工作

标签 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);
        }

最佳答案

最好在您的onclick 处理程序中返回false。该链接通常指向您希望它显示的页面,而弹出脚本在事件处理程序中调用。当您在函数中返回 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;
}

Then you can call it like this (notice the additional return keyword):

<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>

关于javascript - 弹出窗口在一个浏览器上工作,但在另一个浏览器上不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30075242/

相关文章:

javascript - InsertOrderedList 不会创建带有标记文本的新列表

javascript - 使用 $cordova Oauth 问题登录

javascript - Openlayers - 在 map 上的弹出窗口中显示 geojson 文件中的数据

java - 如何让弹窗背景模糊

javascript - $ ("id").trigger ("click") 不调用 :active selector associated with that element

javascript - React Native 嵌套的 ScrollView 锁定

javascript - 在 <frameset> 中设置 <frame> 的 URL 的最佳方法是什么

android - 使用 MVVM 模型在 Xamarin Android 中弹出 View

android - 在可扩展列表中选择子项时如何合并弹出窗口

ios - 我可以在 ios sdk 的警报 View 中添加单选按钮吗?