473,387 Members | 1,510 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

DOM script questions

(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
3 1355
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


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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Jonas | last post by:
Good day, I have a .bat script I'm invoking thru a cgi script (PERL) with (NT/IIS) on a internal network. The .bat script is having problems accessing network drives, it keeps saying "Invalid...
0
by: Mark Griffin | last post by:
Hi... I am trying to convert a simple FAQ script to access mysql db rather than the Access db it was designed for. The originators of the script offer no support at all. I have managed to set...
14
by: Rich | last post by:
I have a project that I would Greatly Appreciate some direction. My goal is to create a "Sales Assistant Script" that recommends products based upon Response's to questions. I know enough...
2
by: Efrat Regev | last post by:
Hello, I'm a data-structures course TA trying to write a python CGI script for automatically compiling and testing students' projects. Unfortunately, I've run into some questions while writing...
0
by: ZMan | last post by:
Scenario: This is about debugging server side scripts that make calls to middle-tier business DLLs. The server side scripts are legacy ASP 3.0 pages, and the DLLs are managed DLLs...
5
by: Moskie | last post by:
I recently came across an application where the 'src' attribute of a script tag was an ASPX file. Like so: <script language="JavaScript" src="scriptfile.aspx"></script> This is a new idea for...
9
by: Jerim79 | last post by:
Here it is: <?php if($_SERVER=='POST'){ $Number=$_POST; $Email=$_POST; $Number2=0; $error=0;
3
by: Jerim79 | last post by:
Here it is: <?php if($_SERVER=='POST'){ $Number=$_POST; $Email=$_POST; $Number2=0; $error=0;
1
by: kvazar | last post by:
What I am trying to create is basically a page with a bunch of multiple choice questions. Basically just like a test page. I am providing the code lower. All the questions that need to show as a part...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.