View Full Version : A contentDocument.readyState Hack

01-16-2012, 05:10 PM
I needed to know if a remote Page was completely loaded in an iframe.
If I try to use framename.contentDocument.readyState=='complete'
I get an permissions error.
However if a create a local Page such as:

<iframe src="http//:somewhere.com" > </iframe>
I can check whether the local Page, hence the remote Page I was interested in is loaded.

Will this hack go away or does it conform to the standard?

01-16-2012, 05:18 PM
an iframe should fire an onload event when it loads a new resource, the way images do when you change their source.

No need to read the content.

01-16-2012, 06:38 PM
Thanks for your quick reply. Indeed it does fire an event and it may well be that I am going about things the wrong way but I am trying to synchronize the loading of two div(s).

In more detail, my code, much abbreviated, is
function setURL(aURL){
document.getElementById("theiframe").src = aURL;
//I need to wait until the aURL is completely loaded.

I understand that javascript does not have the Threading I would normally use.

01-16-2012, 07:03 PM
You can't access the Document object for a page from a different domain because of the same-origin policy. Regarding Jerrold's hack, I'm not sure that an onload event for the local page guarantees that the page in an embedded iframe is also fully loaded -- an embedded iframe might be loaded asynchronously.

mrhoo's solution is a good one. Even though the onload event is nonstandard for an iframe, it's universally supported by modern browsers. Just make sure that you register the event handler immediately after creating the iframe (not in an onload handler) so that you don't miss the event. The following code works:

<iframe src="http://example.com" id="myFrame"> </iframe>
document.getElementById ("myFrame").onload = function ()
alert ("iframe loaded");

01-16-2012, 07:54 PM
Thanks mjy!

It does, indeed work!

I did not know how to register the handler when I had first tried mrhoo's approach.