如何绕过相同的源策略错误来引用iframe中加载的元素?

时间:2017-07-28 04:21:42

标签: javascript jquery iframe same-origin-policy

有一个iframe具有以下结构,通过外部网站external.com加载并在我的域example.com托管:

<body> <!-- this is the body of my html from where I'm trying to make the reference -->
    <iframe>
      #document
          <html>
              <body>      
                  <script type="text/html" id="icon-close"><div class="icon icon-close">      
                      <div class="icon icon close"</div>  
                  </script>
              </body>
          </html>
    </iframe>      
</body>

我特意尝试在这种情况下选择icon-close元素。

我可以使用$("iframe")选择iframe但是当我尝试访问检查器代码中的内容以使用$("iframe").contents()选择元素时,我会收到以下内容:

 DOMException: Failed to read the 'contentDocument' property from 
'HTMLIFrameElement': Blocked a frame with origin 
"https://external.com" from accessing a cross-origin frame.

我尝试在引用之前设置domain.document = 'example.com'

 DOMException: Failed to set the 'domain' property on 'Document': 
'example.com' is not a suffix of 'external.com'.

我也尝试将其设置为external.com

DOMException: Failed to read the 'contentDocument' property from 
'HTMLIFrameElement': Blocked a frame with origin "http://example.com" from 
accessing a cross-origin frame.

我见过相关帖子herehere,但不明白如何解决这个问题。

如何绕过相同的原始政策来访问iframe中的元素?

0 个答案:

没有答案