gpt4 book ai didi

javascript - 从两级深度 iframe 获取浏览器 URL

转载 作者:行者123 更新时间:2023-11-30 12:07:08 25 4
gpt4 key购买 nike

另一个iframe中有一个iframe。我想从第二个 iframe 获取浏览器的 URL (例如:http://test)

//browser URL: http://test <-- I need this URL

<iframe src="http://level1">
<iframe src="http://level2">
<!-- some content -->
</iframe>
</iframe>

我尝试了 document.referrer 但它返回了第一个 iframe 的 URL:

<iframe src="http://level1">
<iframe src="http://level2">
console.log(document.referrer); //It gives back http://level1
</iframe>
</iframe>

更新:

top.location.href 不起作用,因为存在不同的域。不同的浏览器,第一个和第二个 iframe 域。 (我们的网站 - 广告服务器 - 第三方网站)

最佳答案

如前所述,跨域 iframe 通常是不可能的,但对于 Chrome,您可以使用 window.location.ancestorOrigins 为您提供“顶级 url”和如果您在多个 iframe 中,则列出所有域,无论它们是否跨域。

关于javascript - 从两级深度 iframe 获取浏览器 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34877390/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com