如何在新的浏览器选项卡中检测URL更改并取回URL? [英] How to detect url change in a new browser tab and get back the url?
问题描述
我有一个链接,它将在新标签页中打开一个网址,如下所示:
I have a link that will open a url in a new tab like this:
<a href="#" target="teamViewer" style="color: deepskyblue"><span data-bind="text: connectorLoginLink, visible: shouldShowConnectorLoginLink, click: openTeamViewerUrl"></span></a>
由于我想检测新选项卡中的URL更改,因此我认为必须使用window.open(url)来打开此链接.然后,跟踪新窗口.
As I want to detect the URL change in the new tab, I thought I had to use window.open(url) to open this link. Then, keep track of the new window.
在视图模型中:
public openTeamViewerUrl() {
var newWindow = window.open('http://www.google.com', '_blank', 'location=yes');
}
我想将打开的选项卡设置为newWindow对象,然后使用类似以下的方式对其进行监视:
I want to set the opened tab to a newWindow object, then monitor it by using something like this:
var currentPage = newWindow.location.href;
setInterval(function () {
console.log(currentPage);
if (currentPage !== newWindow.location.href) {
// page has changed, set new page as 'current'
currentPage = newWindow.location.href;
console.log(currentPage);
// do other thing...
}
}, 1000);
尝试window.open时出现错误:阻止打开' http://www.google.com/",因为该请求是在未设置允许弹出"权限的沙盒框架中进行的.
I got the error back when trying window.open: Blocked opening 'http://www.google.com/' in a new window because the request was made in a sandboxed frame whose 'allow-popups' permission is not set.
真的被卡住了.如何绕过allow-popups?可以使用其他方法来实现此功能吗?
Really got stuck on this. How to by pass the allow-popups? Can this feature be achieved using other methods?
推荐答案
要检测URL的更改,可以在 window
上使用javascript unload
事件,请参见
To detect a change of URL you can use javascript unload
event on the window
, see https://developer.mozilla.org/en-US/docs/Web/Events/unload
window.addEventListener('unload', function(event) {
console.log('Navigation occuring');
});
这篇关于如何在新的浏览器选项卡中检测URL更改并取回URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!