iframe 子页面获取父页面的url 有时候获取不到完整的,有时候只有第一次能获取到

`function getParentUrl () {
var url = null;
if (parent !== window) {

try {  
  url \= window.parent.location.href;  
} catch (e) {  
  url \= document.referrer;  
}  

}
copyToClip(decodeURI(url))
console.log('复制的url是:::',decodeURI(url))
return decodeURI(url)
}`
image.png
如题。我有时候第一次能获取到,有时候一直都获取不到,但是我想要,我每次执行上面的方法都能及时获取完整的父页面的url(带参数)。我是哪里写错了吗

阅读 7k
2 个回答

这篇文章可能能帮到你:https://humanwhocodes.com/blog/2013/04/16/getting-the-url-of-an-iframes-parent/

多看看几个评论感觉有可以帮到你的。

document.referrer is empty, when the window and its parent differs in protocols (http/https)

document.referrer only reflects the hostname of the window.parent, in multiple embedded iframes,
wherewindow.top!= window.parent you can't query the top hostname

Webkit implemented the location.ancestorOrigins property, which is just an array of hostnames going upwards through the embedding frames, but ideal for blacklisting or whitelisting your content

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题