By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,770 Members | 1,393 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,770 IT Pros & Developers. It's quick & easy.

DOM script questions

P: n/a
(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!

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Jul 23 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Christopher Benson-Manica <at***@nospam.cyberspace.org> writes:
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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #2

P: n/a


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.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #3

P: n/a
Lasse Reichstein Nielsen <lr*@hotpop.com> spoke thus:
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 :)

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Jul 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.