阻止parent.window.opener.location中的JavaScript

Prevent JavaScript in parent.window.opener.location

本文关键字:中的 JavaScript location opener parent window 阻止      更新时间:2023-09-26

当使用此JavaScript代码点击外部链接时,我试图阻止页面关闭。代码正在另一个页面中运行。

if(parent.window.opener) parent.window.opener.location='//www.domain.com';

我有很多链接,很难删除或使用窗口。卸载之前很烦人。

有什么代码可以做到这一点吗?

谢谢。

使用此

    var domain = function(url) {
        return url.replace('http://','').replace('https://','').split('/')[0];
    };
    return domain(location.href) !== domain(url);
}

在这里使用它可以防止链接的默认行为:

document.getElementsByTagName('a').addEventListener("click", function(e){
if(domain(e.target.href)){
e.preventDefault();
}
}, false);