简体   繁体   English

如何检查浏览器中的 url 是否已被 iframe 更改

[英]How to check if url in browser has changed by iframe

I have an iframe (hosted somewhere else) that changes my url in browser upon clicking a button inside the iframe.我有一个 iframe(托管在其他地方),单击 iframe 内的按钮后,它会在浏览器中更改我的 url。

In the browser the url will go from https://www.siteurl.com/#/login to https://www.siteurl.com/#/account在浏览器中 url 将 go 从https://www.siteurl.com/#/loginhttps://www.siteurl.com/#/account

I have tried:我试过了:

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

But this only works if you access https://www.siteurl.com/#/account directly.但这仅在您直接访问https://www.siteurl.com/#/account时有效。

Then I thought about the.on("load") function然后想到了.on("load") function

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

But that does not work at all.但这根本不起作用。 Accessing the iframe is a pain as well because of cross-origin.由于跨源,访问 iframe 也很痛苦。 I have no control over the iframe (except CSS), so this might be a dead end.我无法控制 iframe(CSS 除外),所以这可能是一条死胡同。

If all you want is the current URL the iframe is displaying then this should allow you to access it:如果你想要的只是当前 URL iframe 正在显示那么这应该允许你访问它:

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

The current URL of the browser is by principle accessible under浏览器当前URL原则上可以访问

window.location.href

If these do not work, you might need to break out of the iFrame - see eg link如果这些不起作用,您可能需要断开 iFrame - 参见例如链接

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

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