弹出窗口只在safari中“弹出”一次

时间:2012-02-10 01:24:01

标签: javascript html safari

我有一个JavaScript触发的弹出窗口,它是在点击标签的onclick事件时创建的:

top.popUp('/bus/popup.asp', '', 'height=457px,width=525px,status=no,scrollbars=no,resizable=yes,location=no,menubar=no,toolbar=no');return false;">

function popUp(url, winName, params, win)
{
    var winExists = false;

    if (win.handler)
    {
        if (!(win.handler == "" || win.handler.closed || win.handler.name == undefined))
        {
            winExists = true;
        }
    }   

    if (!winExists)
    {
        win.handler = window.open(url, winName, params)
    } 
    try
    {
        win.handler.focus();
    }
    catch (e)
    {
    }
    return false;
}

我还在主文件中找到了这两个时髦的小函数,在弹出窗口被卸载时触发:

// Hack for onUnload and onBeforeUnload
onBeforeUnloadHappened = false;
closeDirectly = false;

function onBeforeUnloadHandler()
{
if (!closeDirectly)
{
    openerUnhilightSaveLink();
    onBeforeUnloadHappened = true;
}
}
function onUnloadHandler()
    {
if (!closeDirectly)
{
    if (!onBeforeUnloadHappened)
    {
        openerUnhilightSaveLink();
    }   
    else
    {
        onBeforeUnloadHappened = false;
    }
}   
}

问题是在Safari中,窗口只会弹出一次,每次后续点击链接都会让你nada,zilch,没有...我该如何纠正?

我确实找到了几篇文章,谷歌是我的朋友,在线他们建议我添加this.close(),我对卸载事件做了但是它似乎没有解决问题。

我觉得奇怪的另一件事是每次卸载页面时onUnloadHandler()实际上都不会发生!不管发生了什么其他事情,不应该每次都被页面本身调用吗?!

由于

1 个答案:

答案 0 :(得分:0)

我认为您的弹出代码编写不正确。这适用于Safari,因为我刚测试了它。这就是我所做的。

// Create a global popup object as you should rarely ever
// have more than one popup open at a time.
window.popUpObj = null;

// The function to call the popup.
function popUp(url, winName, params)
{

  // If the window has not been defined or if it has been closes open it.
  // Since we defined window.popUpObj to be null we'll expect only 2 conditions.
  if(window.popUpObj == null || window.popUpObj.closed)
  {
    window.popUpObj = window.open(url, winName, params);
  }

  // Get window focus
  window.popUpObj.focus();

  // Return false.
  // we do this so that when we call it we don't need a ; to return false afterwards
  // we just add return before the function call.  It's a bit more elegant.
  return false;
}

你可以这样称呼它。顺便说一句,你应该总是使用某些东西填充第二个参数。

<a href="#" onclick="return popUp('/bus/popup.asp', '_blank', 'height=457px,width=525px,status=no,scrollbars=no,resizable=yes,location=no,menubar=no,toolbar=no');">Link</a>

这是一个有效的HTML文档。

<html>
  <head>
    <script type="text/javascript">

   window.popUpObj = null;

    function popUp(url, winName, params)
    {
      // If the window has not been defined or if it has been closes open it.
      if(window.popUpObj == null || window.popUpObj.closed || window.popUpObj.name == undefined)
      {
        window.popUpObj = window.open(url, winName, params);
      }

      // Get window focus
      window.popUpObj.focus();

      // Return false.
      return false;
    }
    </script>
  </head>
  <body>


    <a href="#" onclick="return popUp('http://www.google.com/', '_blank', 'height=457px,width=525px,status=no,scrollbars=no,resizable=yes,location=no,menubar=no,toolbar=no');">Link</a>

    <a href="#" onclick="window.popUpObj.close(); return false;">close</a>

  </body>
</html>