下面是Google版本,用于从iFrame获取父窗口的URL。
window.location.ancestorOrigins;
我正在寻找相当于上述声明的FireFox和IE。是否有可能做到这一点。
尝试与document.referrer
也只提供iFrame网址。
发布于 2019-06-26 20:30:37
不幸的是,FireFox和IE没有与ancestorOrigins等价的功能。
获得父URL的最佳方法是document.referrer
,不幸的是,如果您正在处理iframes,这意味着您可能无法访问外部页面并获得网页的真实网址。
发布于 2022-07-21 11:45:20
我做了这样的事:
function getAncestorOrigins() {
if (location.ancestorOrigins !== undefined) {
return [...location.ancestorOrigins];
}
const urls = [];
let parentWin = window;
while (parentWin !== window.top) {
if (parentWin.document.referrer) {
try {
const url = new URL(parentWin.document.referrer);
urls.push(url.origin);
} catch (e) {
// console.error
}
}
// @ts-ignore
parentWin = parentWin.parent;
}
return urls;
}
发布于 2022-04-08 10:04:28
我找到了另一种解决方案。这对我有用..。
var url = window.location != window.parent.location ?
document.referrer :
document.location.href;
https://stackoverflow.com/questions/34742800
复制相似问题