我创建了以下代码,可以在 Firefox 和 Chrome 中顺利运行。在 IE8 中,window.opener.onbeforeunload = null 行不会触发。谁能给我建议如何为 IE8 获得类似的结果,即从子窗口停用父窗口的所有先前 onbeforeunload 设置,然后更改(父)window.location?
谢谢!
// this is just the activation of the site's onbeforeunload...
jQuery(document).ready(function(){
var preventUnloadPrompt;
jQuery('form').live('submit', function() { preventUnloadPrompt = true; });
window.onbeforeunload = function() {
var rval;
if(preventUnloadPrompt) {
return;
} else {
return 'Do you want to continue?';
}
return rval;
}
//Now when clicking a button on the main site, a popup is opening
//via popup = window.open{...}. In the popup there is another
//button "Test" which should set the parent window's onbeforeunload
//to zero and load another website in there. The function test() is
//defined in the header, but here it is again to make reading easier:
//function test(){window.opener.onbeforeunload = null;
//window.opener.location="http://www.google.com";}
$("#button").click(function(){
var content = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" dir="ltr"><head><title>Popup</title><script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js?v=1.4.4"></script><script type="text/javascript">function test(){window.opener.onbeforeunload = null; window.opener.location="http://www.google.com";}</script></head><body><div id="popup-message-stopbutton"><input type="button" value="Test" onclick="test()" /></div></body></html>';
var popup = window.open('','', 'width="100",height="100"');
popup.document.write(content);
popup.document.close();
});
});