Getting the document object of an iframe

Try the following

var doc=document.getElementById("frame").contentDocument;

// Earlier versions of IE or IE8+ where !DOCTYPE is not specified
var doc=document.getElementById("frame").contentWindow.document;

Note: AndyE pointed out that contentWindow is supported by all major browsers so this may be the best way to go.

  • http://help.dottoro.com/ljctglqj.php

Note2: In this sample you won’t be able to access the document via any means. The reason is you can’t access the document of an iframe with a different origin because it violates the “Same Origin” security policy

  • http://javascript.info/tutorial/same-origin-security-policy

Leave a Comment