Javascript弹出窗口 - 在FF& Chrome,在IE中失败

时间:2011-12-30 02:29:56

标签: javascript html

我似乎无法弄清楚为什么以下简单的弹出窗口在IE9中不起作用。 FF& Chrome弹出按预期弹出,但单击链接时IE似乎没有做任何事情。我尝试了IE9调试器,但没有得到任何有用的信息。

在头部:

<script type="text/javascript" language="JavaScript">
    function JSPopup(linkref) {
        window.open(linkref,"Report Definitions","width=600,height=480");
        return false;
    }

身体:

<a href="#" onClick="JSPopup('./Web Report Definitions.html')"> <strong>Report Definitions</strong> </a> 

感谢您的帮助,

菲利普

2 个答案:

答案 0 :(得分:2)

原来问题是弹出窗口的名称 - IE不允许空格,FF&amp; Chrome确实:

window.open(linkref,"Report Definitions","width=600,height=480"); 

需要更改为:

window.open(linkref,"ReportDefinitions","width=600,height=480"); 

这适用于各种浏览器。

菲利普

答案 1 :(得分:1)

这是IE6中进行的安全性更改的一部分。现在,您只能在用户启动的事件中调用“window.open”。例如,您的代码将在元素的onclick事件中起作用。 “window.open”http://msdn.microsoft.com/en-us/library/ms536651(VS.85).aspx MSDN页面说明了这一点:

  

“此方法必须使用用户启动的操作,例如单击链接或链接到链接并按Enter键,以打开弹出窗口.Internet Explorer 6中的弹出窗口阻止程序功能会阻止Windows在没有被用户发起的情况下打开。“

示例...

function popUpWin(url,wtitle,wprop){
   if (!window.open){ return; } // checking if we can't do this basic function

   // Kill if(win), since win was a local var this was a usless check
   var win = window.open(url,wtitle,wprop);

   // next line important in case you open multiple with the same 'wtitle' 
   // to make sure the window is reused and refocused.
   if (win && win.focus){ win.focus(); }

   return false;
}