弹出窗口无法正常工作

时间:2012-03-09 11:21:10

标签: javascript

onclick功能无法正常工作,因为即使关闭后,它也会禁用父窗口上的所有链接。 关闭弹出窗口时,父页面链接不再起作用。其他一切都很好。

我的代码是: -

             function MyPopUpWin(message) {
             var iMyWidth;
         var iMyHeight;
         //half the screen width minus half the new window width (plus 5 pixel borders).
           iMyWidth = (window.screen.width/2) - (75 + 10);
         //half the screen height minus half the new window height (plus title and status bars).
          iMyHeight = (window.screen.height/2) - (100 + 50);
         //Open the window.
 var generator = window.open();
document.onclick=function()   {
    try{
        generator.focus();
        return false
    }
    catch(e){}
}

generator.document.write('<html><head><title>Pop uP</title>');
generator.document.write('<p style="color:#C52B27;">');
generator.document.write(message);
generator.document.write('</p>');
generator.document.write('</head><body>');

 generator.document.write('<a href="javascript:self.close()"><img src="/img/save_orange.gif" border=0">  <\/a>');
generator.document.write('</body></html>');
generator.document.close();
 }

2 个答案:

答案 0 :(得分:1)

弹出窗口关闭后,您必须从onclick删除document - 事件处理程序。

<强>编辑:

我不知道哪个比Gaby的答案更好,但试试这个:

document.body.onfocus=function (){generator.focus();return false;}
退出弹出窗口时

然后取消onfocus

或者只是在父窗口上使用(部分?)透明封面div,同时弹出窗口打开。

第2版

甚至可以使用showModaDialog()作为弹出窗口,但此解决方案需要将其他文件加载到模态对话框中。

答案 1 :(得分:1)

关闭窗口后,对它的引用不会丢失......并且.focus()方法不会失败..因此try / catch没有按照您的想法进行操作..

<a href="javascript:window.opener.document.onclick = null;self.close();">
相关问题