簡體   English   中英

關閉Window.open時觸發事件

[英]Trigger event on closing of Window.open

嗨我必須在window.open中加載一個url,該url在我的瀏覽器中保存一些cookie,我想在該window.open關閉后訪問它。 我找不到辦法去做。 我試過這些代碼。

 var windowStatus = window.open(returnURL, "Login", "type=fullWindow,toolbar=yes,scrollbars=yes,resizable=yes,fullscreen");
 windowStatus.addEventListener("beforeunload", (event) =>{
      let cookieVaal = this.cookieService.get('QSTID');
      sessionStorage.setItem('QSTID', cookieVaal);
    })

這也行不通,請給我一些建議。 提前致謝..

var new_window = window.open('url')
new_window.onbeforeunload = function(){ my code }

在設置cookie和訪問cookie時,OP可能會遇到“同源”限制。 這是我在個人網絡服務器上運行的一些JavaScript:

    var url = "http://localhost/exp/alpha.php";
    var strWindowFeatures = "menubar=yes,location=yes,resizable=yes,scrollbars=yes,status=yes";
    var new_window = window.open(url,'Alpha',strWindowFeatures);
    new_window.document.cookie = "name=rose";

    // okay:
    //new_window.onbeforeunload = function(){ alert(document.cookie)};

    // better; brings up dialog box to confirm leaving page
    new_window.addEventListener('beforeunload', (e) => {
      alert(document.cookie);

     // Cancel the event
  e.preventDefault();
    // Chrome requires returnValue to be set
 e.returnValue = true;
    });

只要指定的URL與執行的JavaScript代碼位於同一Web服務器上,腳本就會運行。

更多關於beforeunload

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM