您好,我必须加载一个网址在window.open,网址保存在我的浏览器中的一些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);
})这也不起作用,请给我一些建议。提前谢谢..
发布于 2019-03-20 15:29:46
var new_window = window.open('url')
new_window.onbeforeunload = function(){ my code }发布于 2019-03-20 16:23:29
OP在设置和访问cookie时可能会遇到"Same Origin"限制。以下是我在个人Here服务器上运行的一些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的信息。
https://stackoverflow.com/questions/55255493
复制相似问题