DOM script questions

  • Thread starter Christopher Benson-Manica
  • Start date
C

Christopher Benson-Manica

(Sorry for the duplicate post - I was going to change the subject
before posting and forgot...)

I'm trying to create script that can dynamically execute script from a
given source and then call a callback function when complete. I have
the following...

function bar() {
alert( 'bar' );
}
function foo() {
var s=document.getElementById( 'dynamicScriptElement' );
if( !s ) {
s=document.createElement( 'script' );
s.type='text/javascript';
s.id='dynamicScriptElement';
document.documentElement.childNodes[1].appendChild( s );
}
s.src='test.js';
bar();
}

1) Is the script in test.js guaranteed to be executed before bar() is
called?
2) foo() assumes that the <html> element has a <head> and <body> -
presumably that's reasonable, right?
3) Any other comments would be appreciated!
 
L

Lasse Reichstein Nielsen

Christopher Benson-Manica said:
1) Is the script in test.js guaranteed to be executed before bar() is
called?

There is no standard saying what must happen when you load scripts
after the page has finished loading (and I'm not sure there are
any for before either), so guarantees are hard to make.

My guess is that you can be almost certain that the script has *not*
been loaded.
2) foo() assumes that the <html> element has a <head> and <body> -
presumably that's reasonable, right?

Not necessarily. If the HTML page is a frameset page, it need not have
a body. Otherwise, it's a fairly safe bet, if the HTML page is valid.
3) Any other comments would be appreciated!

Not all browsers allow loading scripts later. Opera started supporting
it somewhere in the 7 series.

Why:
document.documentElement.childNodes[1].appendChild( s );
(which only works in IE in standards mode, not quirks) and not just
document.body.appendChild(s)
Don't assume that childNodes[1] is head or body, it can be a text
node (which would make appendChild fail).

The only way to be certain that a script has loaded is to have that
script call the callback itself.

/L
 
M

Martin Honnen

Christopher Benson-Manica wrote:

I'm trying to create script that can dynamically execute script from a
given source and then call a callback function when complete. I have
the following...

function bar() {
alert( 'bar' );
}
function foo() {
var s=document.getElementById( 'dynamicScriptElement' );
if( !s ) {
s=document.createElement( 'script' );
s.type='text/javascript';
s.id='dynamicScriptElement';
document.documentElement.childNodes[1].appendChild( s );
}
s.src='test.js';
bar();
}

1) Is the script in test.js guaranteed to be executed before bar() is
called?

No, browsers can and in reality do load the script file asynchronously
so you set src and that triggers loading the script (or for instance
loading an image if you had new Image().src = 'whatever.gif') but then
the script exection continues meaning the bar(); call is executed.
So you need an onload or onreadystatechange event handler on the script
element, Mozilla and IE should support that, not sure about other browsers.
2) foo() assumes that the <html> element has a <head> and <body> -
presumably that's reasonable, right?

So
document.documentElement.childNodes[1]
is assumed to be the <body> element? That is not reasonable. Depending
on white space text node parsing the number of child nodes in an element
differs between the different DOM implementations browsers have.

If you want to script the <body> element use document.body (as long as
you are dealing with text/html content at least) but of course
document.body is only an object once the opening <body> tag has been
parsed thus if you intended to call foo earlier you were in trouble.
 
C

Christopher Benson-Manica

Lasse Reichstein Nielsen said:
My guess is that you can be almost certain that the script has *not*
been loaded.

I suppose, based on Martin's response, that you are correct - I just
had something simple in test.js to test, so I suppose that's why it
appeared to execute before bar().

Not all browsers allow loading scripts later. Opera started supporting
it somewhere in the 7 series.

Do you know whether IE 5.0 supports this?
(which only works in IE in standards mode, not quirks)

Sounds like no :(
The only way to be certain that a script has loaded is to have that
script call the callback itself.

Well that's unfortunate. Then again this whole situation is
unfortunate :)
 

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,981
Messages
2,570,188
Members
46,731
Latest member
MarcyGipso

Latest Threads

Top