检测所在窗口是否为最外层的窗口,若不是则跳脱包含它的框架
if( window !== window.top ) {
window.top.location = location;
}
top 对象指向最外层,浏览器窗口本身,self 总是等于window
当Iframe page和 containing page 来自同源时,parent.location or top.location可以获取containing 页的地址
参考
Getting the URL of an iframe’s parenthttp://www.nczonline.net/blog/2013/04/16/getting-the-url-of-an-iframes-parent/
http://www.nczonline.net/blog/2009/09/15/iframes-onload-and-documentdomain/
不同源的话
来自另一个域名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;
}
时间: 2024-10-05 20:55:30