-1

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();
});

});

4

2 に答える 2

0

次のように、jQueryを使用してオープナーにアクセスし、イベントのバインドを解除してみてください。

$(window.opener).unbind('onbeforeunload');
于 2012-10-31T08:21:44.290 に答える
0

これは古い投稿だと思いますが、onbeforeunloadを子から親ウィンドウでアンバインドする方法を共有したいと思いました。うまくいけば、それは誰かを助けるでしょう。

親ウィンドウのjsで、イベントのバインドを解除する関数を記述します

function unbindOBE(){
     $(window).unbind('beforeunload');
}

子ウィンドウのjsで、リロードまたは割り当てを行う前、または何を持っているかを確認する前に、関数を呼び出すだけです。

window.opener.unbindOBE();

そうは言っても、場所を変更しようとしている場合は、

window.opener.location = "http://www.google.com";

割り当てまたは置換メソッドのいずれかを使用した場合は、はるかにうまく機能します

window.opener.location.assign("http://www.google.com");

私はIEでこれを試したことがないのでYMMV

于 2014-01-14T01:20:27.730 に答える