Javascript 从IFRAME获取当前URL
有没有一种简单的方法可以从iframe获取当前URL 浏览者将浏览多个站点。Javascript 从IFRAME获取当前URL,javascript,url,iframe,Javascript,Url,Iframe,有没有一种简单的方法可以从iframe获取当前URL 浏览者将浏览多个站点。 我猜我会在javascript中使用一些东西。出于安全原因,只要iframe和引用javascript的内容来自同一个域,您就只能获取url。只要这是真的,像这样的事情就会起作用: document.getElementById("iframe_id").contentWindow.location.href 如果这两个域不匹配,您将遇到跨站点引用脚本安全限制 另请参见。如果您的iframe来自另一个域(跨域),其他
我猜我会在javascript中使用一些东西。出于安全原因,只要iframe和引用javascript的内容来自同一个域,您就只能获取url。只要这是真的,像这样的事情就会起作用:
document.getElementById("iframe_id").contentWindow.location.href
如果这两个域不匹配,您将遇到跨站点引用脚本安全限制
另请参见。如果您的iframe来自另一个域(跨域),其他答案将不会帮助您。。。您只需使用以下方法:
var currentUrl = document.referrer;
还有-这里是主url 希望这能对你的情况有所帮助, 我遇到了完全相同的问题,只是使用localstorage在父窗口和iframe之间共享数据。 因此,在父窗口中,您可以:
localStorage.setItem("url", myUrl);
在iframe源代码中,只需从localstorage获取以下数据:
localStorage.getItem('url');
节省了我很多时间。
据我所知,唯一的条件是访问父页面代码。
希望这能帮助到某人。如果您在iframe环境中 你可以
const currentIframeHref = new URL(document.location.href);
const urlOrigin = currentIframeHref.origin;
const urlFilePath = decodeURIComponent(currentIframeHref.pathname);
如果您在父窗口/框架中,则可以使用的答案是
document.getElementById("iframe_id").contentWindow.location.href
我对blob url hrefs有问题。因此,通过对iframe的引用,我刚刚从iframe的src属性生成了一个url:
const iframeReference = document.getElementById("iframe_id");
const iframeUrl = iframeReference ? new URL(iframeReference.src) : undefined;
if (iframeUrl) {
console.log("Voila: " + iframeUrl);
} else {
console.warn("iframe with id iframe_id not found");
}
对于可能遇到此问题的任何人,请提供一些附加信息: 如果您试图在加载iframe之前从iframe获取URL,则会得到空值。 我通过在javascript中创建整个iframe并使用onLoad函数获得所需的值来解决这个问题:
var iframe = document.createElement('iframe');
iframe.onload = function() {
//some custom settings
this.width=screen.width;this.height=screen.height; this.passing=0; this.frameBorder="0";
var href = iframe.contentWindow.location.href;
var origin = iframe.contentWindow.location.origin;
var url = iframe.contentWindow.location.url;
var path = iframe.contentWindow.location.pathname;
console.log("href: ", href)
console.log("origin: ", origin)
console.log("path: ", path)
console.log("url: ", url)
};
iframe.src = 'http://localhost/folder/index.html';
document.body.appendChild(iframe);
由于同源策略,我在访问“跨源”帧时遇到了问题—我通过在本地运行Web服务器而不是直接从磁盘运行所有文件来解决这个问题。为了使所有这些工作正常,您需要使用与源站相同的协议、主机名和端口访问iframe。不确定从磁盘运行所有文件时丢失了/丢失了哪些文件
另外,有关位置对象的详细信息:如果您在没有跨域src的iframe中,或者src为空: 然后:
如果您在具有跨域src的iframe中: 然后:
我一直在尝试从shopify google analytics附加脚本iframe内部检索完整的URL<代码>文档。refferer仅显示主机名,不显示搜索参数。但是我发现了另一个对我有用的属性
document.baseURI
,我链接的问题没有成功的答案。。。如果你指的是最重要的答案,那么这并不是一个有效的解决方案。使用IE,您可以尝试上述问题答案中提供的HTA方法。如果我们将属性sandbox=“allow same origin”
添加到iFrame中会怎么样?如果我们在iFrame的目标服务器上允许新源,我们会遇到问题吗?另一种方法:window.frames[0].location.href
-1 OP查找的是iframe的当前url,而不是父页面的引用人。@Christophe-我知道,但它可以帮助查找主url的人解决此问题!这不会给你最顶级的(尝试2个嵌套的iFrame),但即使只考虑一个级别,也很容易伪造它,因此你不能完全依赖它!这正是我一直在寻找的,它解决了我的问题而没有给我X脚本错误+1为此,非常好,我需要iframe url,而不是父项:)如果您来这里查找iframe的“父项”页面的url,请参阅本地存储仅在同一域上工作。。。那为什么要这么麻烦呢?只需查询location.href
function getOriginUrl() {
var href = document.location.href;
var referrer = document.referrer;
// Check if window.frameElement not null
if(window.frameElement) {
href = window.frameElement.ownerDocument.location.href;
// This one will be origin
if(window.frameElement.ownerDocument.referrer != "") {
referrer = window.frameElement.ownerDocument.referrer;
}
}
// Compare if href not equal to referrer
if(href != referrer) {
// Take referrer as origin
return referrer;
} else {
// Take href
return href
}
}
function getOriginUrl() {
var href = document.location.href;
var referrer = document.referrer;
// Detect if you're inside an iframe
if(window.parent != window) {
// Take referrer as origin
return referrer;
} else {
// Take href
return href;
}
}