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

kidddder
  • 567

`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(带参数)。我是哪里写错了吗

回复
阅读 3.6k
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

宣传栏