如何阅读新标签位置.href.match?使用 Javascript

How to Read New Tab location.href.match? using Javascript

本文关键字:match href 使用 Javascript 位置 新标签 何阅读 标签      更新时间:2023-09-26

我想在新打开的选项卡与我想要的URL匹配时显示警报。

我想出了这个代码。

<button class="button" id="open-newtab"
onclick="return ouvre('https://www.facebook.com/');">Open</button>
<script>
function ouvre(fichier) {
ff = window.open(fichier, "popup", "width=600px,height=300px,left=50%,top=50%");
    if (ff.location.href.match(/https:'/'/www.facebook.com/)) {
     alert("Hello! I am an alert box!!");
} 
}
</script>

当我删除ff.location.href.match上的ff时,它将读取窗口的当前URL,而不是新选项卡。

是否可以读取新选项卡的 location.href?

谢谢

只要打开的窗口与父页面位于同一域中,您就可以访问窗口的 URL。

但是,如果您想打开不同的域,或者一旦用户导航到其他域,您将无法再查询它。