Click to See Complete Forum and Search --> : Simple DOM stuff doesn't work from external script


HataruMendo
08-10-2004, 11:18 PM
Hi all,

I'm starting with the DOM experience and I'd need a hand...

I've prepared some *very simple (6 lines of code in total)* text substitution via DOM in http://www.netropolis-si.com/hataru/test1.htm

As you can see, the sample text is changed automatically from 'Original text' to 'Changed text', and the write tests before and after this operation work OK, too.

However, in
http://www.netropolis-si.com/hataru/test2.htm

I have simply moved the 6 line script to an external file (http://www.netropolis-si.com/hataru/test.js)
and now it doesn't work: first write test is OK, then text substitution doesn't happen, and the second write test is gone too.

Why??????????

Any help will be appreciated.

Regards,
Hataru Mendo.

Pittimann
08-11-2004, 12:24 AM
Hi!

The difference is not only that once your js is external and once not but also the position of the script tags.

In your external version, you call the script, before the div exists. Simply try this:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Testing</title>
</head>
<body>
<div id="doc">
Original text
</div>
<script language="JavaScript" type="text/javascript" src="test.js"></script>
</body>
</html>

Cheers - Pit

HataruMendo
08-11-2004, 06:55 AM
Hi Pit,

Yes you're right. I've created a test3.htm with the js stuff in the external file to see... moving the <script> tag before and after the <div> makes the difference because the code checks for something that's simply not there yet. How can one be so stupid?

Thanks a lot; but now I have one key question:

is there any way to simply link a .js (that will perform text substitution depending upon a certain condition) from the HTML head section and have it execute its cool stuff without NEVER ever referencing it any more in the HTML code?

I mean, just like my test2.htm does, but waiting or making sure somehow its target is already in place.

Thanks in advance.

CU around,
Hataru Mendo.

Pittimann
08-11-2004, 07:03 AM
Hi!

If you put the code to be executed into a function inside the external file and call this function with onload="nameOfFunction()" in the body tag of the document, all your objects will be "ready".

Cheers - Pit

HataruMendo
08-11-2004, 09:53 PM
Again, it's true.

It's amazing what happens when you think you already know all that's written on the 'first steps' manual :-)

Thanks a lot again,
Hataru Mendo.

Pittimann
08-12-2004, 03:14 AM
Hi!

You're welcome.

Regards - Pit

HataruMendo
08-14-2004, 04:52 PM
Hi again,

for completeness' sake I have to say that I have found a clean, final solution to 'run' automatically functions from a linked javascript *once the document is loaded*. Perhaps it may be useful for some other newbies.

Simply include this code in the script:

window.onload = function(e) {
// Your code here...
}

and after that, ALL the script related stuff you must include in your HTML is simply the <script src="..."></script> line, that may very well be in the <head>.

Thanks again Pit.

Regards,
Hataru