Can you see a problem with the DOM ready method?

A

adambones

I am using the following code in my applications for kick-starting
JavaScript applications:

function App(d) {
this.d = d; this.n = d.createElement('div');
}
App.prototype.run = function() {
try {
this.d.body.appendChild(this.n);
this.d.body.removeChild(this.n);
f();
} catch (e) { setTimeout(arguments.callee, 50) }
}

// new App(document).run(function() { alert('Lets go') })

The problem I've had with using DOMContentLoaded is that you must be
sure that the document is *not* ready at the time you attach the event
- otherwise it will never fire, and in the case of FF which has no
readyState property, you cannot check that a document is already
loaded.

This issue is arising for me since my application requires me to check
the ready state of an iframed document from a parent window. In some
cases the framed document is already loaded by the time the parent
window code runs.

I'm posting this here because I'd appreciate if anyone can point out
any pitfalls in this approach. It's seems way too simple and obvious
to me, which leads me to fear I am missing something...
 
A

adambones

Sorry, I posted that code without running it. It should be something
like:

function run(d, f) {
var n = d.createElement('div');
(function() {
try {
d.body.appendChild(n);
d.body.removeChild(n);
f();
} catch (e) { setTimeout(arguments.callee, 50) }
})()

You get the idea anyway...
 
D

David Mark

Sorry, I posted that code without running it. It should be something
like:

    function run(d, f) {
      var n = d.createElement('div');
      (function() {
        try {
          d.body.appendChild(n);
          d.body.removeChild(n);
          f();
        } catch (e) { setTimeout(arguments.callee, 50) }
      })()

You get the idea anyway...

Not really. Is this an assumption that you can catch "Operation
Aborted" errors? You might be able to in IE8 from what I've heard
(never tried), but IE6/7 users will see nothing but a generic error
page. And an append success is not an indication the document is
finished loading and safe for scripting.

The solution is to have the framed document call a predetermined
global "ready" function (or method of an object) provided by the
parent frame.
 
A

adambones

You might be able to in IE8 from what I've heard
(never tried), but IE6/7 users will see nothing but a generic error
page

I'm not sure what you mean - where is an error page coming from?
Either f is run or it isn't (and I'm running it in IE6+).
 And an append success is not an indication the document is
finished loading and safe for scripting.

This is really what I want to know - if it is *not* an indication that
can you shed some light on why or give an example case?
The solution is to have the framed document call a predetermined
global "ready" function (or method of an object) provided by the
parent frame.

In my case I need the child window to be agnostic of the parent. The
application is a CMS - the top window is the editing interface, and
the child can be any website.
 
D

David Mark

 > You might be able to in IE8 from what I've heard


I'm not sure what you mean - where is an error page coming from?
Either f is run or it isn't (and I'm running it in IE6+).

You are trying to insert elements into the body of a document, but you
have no idea what state it is in. The IE catastrophe I referred to is
known to be sporadic (as you might expect with something like this).
Google for IE and "Operation Aborted".
This is really what I want to know - if it is *not* an indication that
can you shed some light on why or give an example case?

It is not an indication because it may succeed while the document is
still parsing (and therefore dangerous to manipulate). The only thing
holding it back is when the body hasn't started parsing (e.g. script
running in the head), document.body is null. That's the error you're
catching. As soon as the body starts parsing, your code will succeed
in many browsers (even sporadically in IE).

In my case I need the child window to be agnostic of the parent. The
application is a CMS - the top window is the editing interface, and
the child can be any website.

If the child can be any Website, how do you plan to manipulate it with
script?
 
T

Thomas 'PointedEars' Lahn

adambones said:
I am using the following code in my applications for kick-starting
JavaScript applications:
[...]
I'm posting this here because I'd appreciate if anyone can point out
any pitfalls in this approach. It's seems way too simple and obvious
to me, which leads me to fear I am missing something...

What do users do without script support? What are search robots going to
find there?


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top