Cross-browser Access html content of object tag

2019-03-03 04:00发布

问题:

I have object tag with a data attribute to a html file from same domain.

I want to access the content of the html.

HTML:

<object id="object" width="420" height="360" data="jsp/index.html"></object>

JS:

var object = document.getElementById("object").contentDocument;
var html = object.getElementById("tmpl").innerHTML

The above code is working in all browsers except IE7

Please help me in accessing the DOM content of object tag in IE7

回答1:

Ok so i tried to play around the code a little as provided by you and i found out some important things, first of which is:

1> I first tried to get the contents of html like this in firebug :

console.log(document.getElementById("iframe").contentDocument.innerHTML);

I am gonna attach my firebug result here :

So you can see that there is a permission denied coming up here.

2> Similarly, i tried out for object also and this is what i got :

From here it is fairly evident that it is failing because of the Same Origin Policy.

I do not know how you are accessing them, but for me this domain and host in not accessible. This is guess, is also the reason for you failing to get the contents of the object id . I suggest that you use Cross-site HTTP requests or the CORS way to get things done here .