chr*_*ris 84 javascript url iframe
有没有一种从iframe获取当前URL的简单方法?
观众将经历多个站点.我猜我会在javascript中使用一些东西.
kky*_*kyy 150
出于安全原因,只要iframe的内容和引用的javascript从同一个域提供,您就只能获取网址.只要这是真的,这样的东西就会起作用:
document.getElementById("iframe_id").contentWindow.location.href
Run Code Online (Sandbox Code Playgroud)
如果这两个域不匹配,您将遇到跨站点引用脚本安全限制.
另见类似问题的答案.
Par*_*Par 25
如果你的iframe来自另一个域,(跨域),其他答案对你没有帮助......你只需要使用它:
var currentUrl = document.referrer;
Run Code Online (Sandbox Code Playgroud)
而且 - 这里你有主要的网址!
我遇到了 blob url href 问题。因此,通过引用 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");
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
167344 次 |
| 最近记录: |