Friday, 23 August 2013

iframe contentDocument: Access is denied

iframe contentDocument: Access is denied

The following code runs as part of a bookmarklet. It creates and iframe
and then tries to access that iframe.
For some sites on IE (tried IE 10) it fails with "Access is denied" for
iframeDoc = i.contentDocument || i.contentWindow.document;
I've put the call inside a setInterval, which seemed to work for some
sites. Apparently they just needed a bit of time.
An example of a site where it does NOT work is
http://www.epicurious.com/recipes/food/views/Rum-Raisin-Apple-Pie-236430
var i=document.createElement('iframe');
document.body.appendChild(i);
var intervalID = setInterval( function(){
try{
var iframeDoc = i.contentDocument || i.contentWindow.document;
clearInterval(intervalID);
alert("success!");
} catch(e){}
}, 10 );

No comments:

Post a Comment