首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Window.open关闭时触发事件

Window.open关闭时触发事件
EN

Stack Overflow用户
提问于 2019-03-20 15:27:02
回答 2查看 700关注 0票数 0

您好,我必须加载一个网址在window.open,网址保存在我的浏览器中的一些cookie,我想访问它后,window.open是关闭的。我找不到办法做这件事。我试过这些代码。

代码语言:javascript
复制
 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);
    })

这也不起作用,请给我一些建议。提前谢谢..

EN

回答 2

Stack Overflow用户

发布于 2019-03-20 15:29:46

代码语言:javascript
复制
var new_window = window.open('url')
new_window.onbeforeunload = function(){ my code }
票数 1
EN

Stack Overflow用户

发布于 2019-03-20 16:23:29

OP在设置和访问cookie时可能会遇到"Same Origin"限制。以下是我在个人Here服务器上运行的一些JavaScript:

代码语言: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的信息。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55255493

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档