繁体   English   中英

如何在新的浏览器选项卡中检测URL更改并取回URL?

[英]How to detect url change in a new browser tab and get back the url?

我有一个链接,它将在新标签页中打开一个网址,如下所示:

<a href="#" target="teamViewer" style="color: deepskyblue"><span data-bind="text: connectorLoginLink, visible: shouldShowConnectorLoginLink, click: openTeamViewerUrl"></span></a>

由于我想检测新选项卡中的URL更改,因此我认为必须使用window.open(url)来打开此链接。 然后,跟踪新窗口。

在视图模型中:

public openTeamViewerUrl() {
    var newWindow = window.open('http://www.google.com', '_blank', 'location=yes');
}

我想将打开的选项卡设置为newWindow对象,然后使用类似以下的方法对其进行监视:

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/ ”,因为该请求是在未设置“允许弹出”权限的沙盒框架中进行的。

真的卡住了。 如何绕过allow-popups? 可以使用其他方法来实现此功能吗?

要检测URL的更改,可以在window上使用javascript unload事件,请参阅https://developer.mozilla.org/en-US/docs/Web/Events/unload

window.addEventListener('unload', function(event) {
    console.log('Navigation occuring');
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM