取得 iframe 容器的 URL

检测所在窗口是否为最外层的窗口,若不是则跳脱包含它的框架
if( window !== window.top ) {
   window.top.location = location;
}
top 对象指向最外层,浏览器窗口本身,self 总是等于window
 
当Iframe page和 containing page 来自同源时,parent.location or top.location可以获取containing 页的地址
 
参考
 
不同源的话
来自另一个域名iframe 里面的内容不能被容器页面的Javascript 操作读取,iframe也不能操作容器页面
the HTTPReferer header for a page inside of an iframe is always set to the containing page’s URL
function getParentUrl() {
    var isInIframe = (parent !== window), //检测是否该页面是否在iframe中
        parentUrl = null;

    if (isInIframe) {
        parentUrl = document.referrer;
    }

    return parentUrl;
}
原文地址:https://www.cnblogs.com/chuangweili/p/5162742.html