【问题标题】:JavaScript - access iframe in iframesJavaScript - 在 iframe 中访问 iframe
【发布时间】:2015-10-02 12:29:39
【问题描述】:

要访问我的主文档中的 iframe,我使用以下代码

// First iFrame in the main document
var iFrame = document.querySelector("iframe");

// Second iFrame in the iFrame
var iFrame_2 = iFrame.contentWindow.document.querySelector("iframe");

// Third iFrame ... here i get a "permission denied error"
var iFrame_3 = iFrame_2.contentWindow.document.querySelector("iframe");

当我尝试访问第三个 iFrame 时,我得到权限拒绝访问文档,为什么?

【问题讨论】:

    标签: javascript iframe window document


    【解决方案1】:

    第二个 iframe 是否链接到外部网站? 他们的服务器可能会阻止 CORS(跨域资源共享),这就是您的第三个 iframe 被拒绝权限的原因。

    【讨论】:

    • 是的,iframe是外部网站,有没有办法改变外部iframe中的元素?
    • 无法更改外部 iframe 中的元素,至少在服务器阻止 CORS 请求时不会。
    • 但是为什么 iframe 请求可以访问它而 XMLHTTPREQUEST 不能访问 url?
    • 因为 iframe 嵌入页面,而 XHR 请求页面。服务器可以根据安全设置对此进行不同的解释。
    猜你喜欢
    • 2010-11-29
    • 1970-01-01
    • 1970-01-01
    • 2021-01-30
    • 1970-01-01
    • 2013-08-03
    • 1970-01-01
    • 2012-07-04
    • 1970-01-01
    相关资源
    最近更新 更多