简体   繁体   English

如何在新的浏览器选项卡中检测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>

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. 由于我想检测新选项卡中的URL更改,因此我认为必须使用window.open(url)来打开此链接。 Then, keep track of the new window. 然后,跟踪新窗口。

In viewmodel: 在视图模型中:

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

I want to set the opened tab to a newWindow object, then monitor it by using something like this: 我想将打开的选项卡设置为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);

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. 尝试window.open时,我得到了错误提示: 阻止在新窗口中打开“ http://www.google.com/ ”,因为该请求是在未设置“允许弹出”权限的沙盒框架中进行的。

Really got stuck on this. 真的卡住了。 How to by pass the allow-popups? 如何绕过allow-popups? Can this feature be achieved using other methods? 可以使用其他方法来实现此功能吗?

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 要检测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