2013-07-03 160 views
0

我需要在父窗口中显示警报,然后在关闭子窗口时刷新它。我想:如何从JavaScript中的子弹出窗口在父窗口中显示警报?

window.onunload = refreshParent; 
     function refreshParent() { 
         window.opener.focus(); 
         window.opener.alert('Testing'); 
         window.opener.location.reload(); 
     } 

这确实显示在父警报,但我猜,因为孩子是在父,子窗口的js冻结,直到我打OK的父窗口。我可以忍受这一点,但问题是,当我显示警报时,父窗口似乎没有得到焦点,所以警报最终隐藏在弹出窗口后面。任何建议家伙?

+0

这是在所有浏览器一样的吗?它可能只是实现特定的。 –

+0

我在FF和Chrome中测试过,并且问题在 – Bluemagica

+0

中都是一样的您是否尝试过使用[window.parent](http://www.w3schools.com/jsref/prop_win_parent.asp),如: window.parent。警报( “消息”); 我不确定这是否能解决您的焦点问题,但值得一试。 –

回答

2

试试这个代码

<!DOCTYPE html> 
<html> 
<head> 
<script> 
function openWin() 
{ 
myWindow=window.open('','','width=200,height=100'); 
myWindow.document.write("<p>This is 'myWindow'</p>"); 
refreshParent(); 
} 

     function refreshParent() { 

         window.parent.focus(); 
         window.parent.alert("message"); 
         window.parent.location.reload(); 
     } 
</script> 
</head> 
<body> 
<input type="button" value="Open 'myWindow'" onclick="openWin()" /> 
</body> 
</html> 
+0

谢谢,我只是使用window.alert而不是alert并解决了问题。 TNX – User1162527

相关问题