gpt4 book ai didi

javascript - 如何使用javascript从父窗口获取子窗口url

转载 作者:行者123 更新时间:2023-12-01 02:41:55 24 4
gpt4 key购买 nike


我正在启动一个带有窗口引用名称的子窗口。我想在子窗口每次变化时捕获它的 URL。

    var winRef;
var url='http://www.google.com';
if(winRef == null || winRef.closed)
{
winRef = window.open(url, "mywindow");
winRef.focus();
alert(winRef.location.href);
}
winRef.captureEvents(Event.CHANGE);
winRef.onchange=function(){
alert('change event triggered');
console.log(winRef.location.href);
}


我已经尝试使用事件 LOAD、CHANGE 来捕获窗口的位置变化。但是 LOAD 和 CHANGE 事件只被触发一次。

我不想使用 setinterval 来检查 window.location.href 的变化,因为它可能会导致性能问题。

有没有其他方法可以获取子窗口 URL?

最佳答案

您无法准确检测到该事件,但您可以采取变通办法并在所有 X 毫秒内检查 URL。

var self = this

var detectsUrl = setInterval(function(){
var a = winRef.document.createElement('a');
a.href = winRef.document.URL;
if (a.hostname == "www.myURL.com") {
winRef.close();
clearInterval(detectsUrl);
self.callback();
};
}, 1000); // Here we check every seconds

关于javascript - 如何使用javascript从父窗口获取子窗口url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17462274/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com