Uncaught SecurityError: Failed to read the 'contentDocument' property from 'HTMLIFrameElement': Blocked a frame with origin “https://localhost”

时光怂恿深爱的人放手 提交于 2019-12-18 11:51:38

问题


I am facing below issue while trying to capture click events of G + follow button.

Uncaught SecurityError: Failed to read the 'contentDocument' property from 'HTMLIFrameElement': Blocked a frame with origin "https://localhost" from accessing a frame with origin "https://apis.google.com". Protocols, domains, and ports must match.


回答1:


I found a similar discussion, Uncaught SecurityError: Failed to read the 'contentDocument' property from 'HTMLIFram.

This issue fired when you try to call ajax to another domain, please check this article for more info about Same origin policy

Mozilla's Same Origin article

For fix this, you will need to add this code

document.domain = 'yourdomain.com'

From the article itself:

A page may change its own origin with some limitations. A script can set the value of document.domain to a subset of the current domain. If it does so, the shorter domain is used for subsequent origin checks. For example, assume a script in the document at http://store.company.com/dir/other.html executes the following statement:

document.domain = "company.com";

After that statement executes, the page would pass the origin check with http://company.com/dir/page.html. However, by the same reasoning, company.com could not set document.domain to othercompany.com.

The port number is kept separately by the browser. Any call to the setter, including document.domain = document.domain causes the port number to be overwritten with null. Therefore one cannot make company.com:8080 talk to company.com by only setting document.domain = "company.com" in the first. It has to be set in both so that port numbers are both null.




回答2:


My solution reconstructs the iframe and usable in angular. When we construct an iframe, it requires origin security check to modify iframe content. This solution allows us to recreate iframe content several times.

HTML

<div id="iframecontainer"></div>

JS

var content = "<h1>Content inside Iframe</h1>"; //desired content of iframe
var iframecontainer = document.getElementById("iframecontainer");
iframecontainer.innerHTML ='<iframe id="threedsframe" width="%90" height="400px"></iframe>';
var iframe = iframecontainer.childNodes[0];
let doc =  iframe.contentDocument || iframe.contentWindow;
doc.open();
doc.write(content);
doc.close();


来源:https://stackoverflow.com/questions/28272933/uncaught-securityerror-failed-to-read-the-contentdocument-property-from-html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!