【问题标题】:How to check if url in browser has changed by iframe如何检查浏览器中的url是否已被iframe更改
【发布时间】:2020-07-27 14:32:27
【问题描述】:

我有一个 iframe(托管在其他地方),点击 iframe 内的按钮后,它会在浏览器中更改我的网址。

在浏览器中,网址将从https://www.siteurl.com/#/login 变为https://www.siteurl.com/#/account

我试过了:

if (window.location.toString().includes("account")){
alert("url contains account");
}

但这只有在您直接访问https://www.siteurl.com/#/account 时才有效。

然后我想到了.on("load")函数

$(function(){
$(window).on("load", function(){
 if (window.location.toString().includes("account")){
 alert("url contains account");
 }
});
});

但这根本不起作用。 由于跨域,访问 iframe 也很痛苦。我无法控制 iframe(CSS 除外),所以这可能是一个死胡同。

【问题讨论】:

    标签: javascript jquery iframe


    【解决方案1】:

    如果你想要的只是 iframe 显示的当前 URL,那么这应该允许你访问它:

    document.getElementById('ID_of_your_iframe').contentWindow.location.href
    

    浏览器的当前URL原则上可以在

    下访问
    window.location.href
    

    如果这些都不起作用,您可能需要跳出 iFrame - 参见例如link

    【讨论】:

    • 改变的不是 iframe SRC。它是浏览器本身的 URL。 iframe 中#document 的内容确实发生了变化
    • 无论我做什么,它都不会检查 url 是否更改。也试过这个if (window.location.href.indexOf("account") != -1) 它只有在你直接访问siteurl.com/#/account 时才有效
    猜你喜欢
    • 2010-12-18
    • 1970-01-01
    • 1970-01-01
    • 2013-02-11
    • 2011-07-13
    • 1970-01-01
    • 1970-01-01
    • 2013-02-22
    • 1970-01-01
    相关资源
    最近更新 更多