Forum Moderators: open

Message Too Old, No Replies

Trouble loading multiple scripts from external.js

         

mrscott

3:38 am on Jan 19, 2004 (gmt 0)

10+ Year Member



I know the answer will be an easy solution but my brain is vapor locked for some reason.
Anyway I have various javascripts on a '.js' file that works fine UNTIL I add this one particular javascript.

For example
<HEAD>
<script language="JavaScript" type="text/javascript" src="external.js">
</script>

<BODY onLoad="javascript:GetTime();>

This works fine unitl I add another javascript to my 'external.js' sans any <script> of course and I get an error, then click OK, then my 'GetTime' script still works but my new script doesn't. (I got the new script from here btw...its an image slide show).

So I just tried to add the new script directly to the <BODY>. What happens there is the 'GetTime' ceases to work and the 'image slider' does work. UGH!

I looked through other posts but I couldn't find anything that matches my prob. Alas I am at a loss. Please be gentle in your reply's, I understand a fair amount but I'd still consider myself a beginner.

Thanks

Purple Martin

3:58 am on Jan 19, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Whenever a browser comes across a JavaScript error, it stops executing any more JavaScript, including all other functions that haven't been executed yet. Once you've found and fixed the error, the other code will start working again - assuming that the other code is error-free ;-)

mrscott

5:50 am on Jan 19, 2004 (gmt 0)

10+ Year Member



Right. But what I'm having trouble with is that each script runs fine independently but not when combined for use on the same web page. Could it be because they are different script versions? If so, its still beyond my capability to fix.

Purple Martin

6:03 am on Jan 19, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



To prove to yourself that it's not the script versions, just remove all the version attributes from your script tags.

Now to work out why they work independantly but not together:

The first thing I would look for is clashing function names or variable names.

Next I'd look for code which is dependant on other code already being executed: if one part hasn't finished yet another part may not work if it needs the results of the first part. I haven't made that very clear, I hope it makes sense.

mrscott

6:39 am on Jan 19, 2004 (gmt 0)

10+ Year Member



I'll give it a go. I dont think it the script version as I've run it every which way, but I remember reading something about that so...

Hmm, renaming functions and variables...ok that sounds easy enough. I haven't seen any books/tutorials/ other help sites that have mentioned that...Cool. People seem to be very helpful on this site and not slam those who are lacking in coding skills.

mrscott

8:07 am on Jan 19, 2004 (gmt 0)

10+ Year Member



Well so far no function names match. I thought this would've fixed the problem for sure <BODY onLoad="GetTime();RotateImage();"> but it didn't, and assuming it did, or can, how then do I get the scripts in there proper location/table?

Back to more searching.

Saintjude

3:18 pm on Jan 19, 2004 (gmt 0)



Hello mrscott,

Without the full details, I'd guess it is indeed an onload conflict of some kind. This must account for more errors in cut'n'paste scripts than all others combined.

There are 2 two ways that your scripts could be grabbing the onload event (which could cause conflicts):

1) <body onload = "someFn( )">
In the HTML. By the way, the 'javascript:' bit should be dropped in your code.

2) window.onload = someFn
In the body of the script. No brackets.

In any combination of these, the last one read will be the only one 'surviving'. The safest option for you, I believe, is to fire onloads from the body tag, putting both function calls in the handler. It looks like you know this already. You could try swapping their order.

A half-decent script will at least avoid using global variables, and if it can't avoid them, will avoid names like a,b,c. A more likely conflict is that both script are using other event handlers.

The most likely candidate overall is Purple Martin's suggestion that one of the scripts is faulty. If they both work on their own however there is a conflict at large.

It seems we aren't allowed to post the locations of our problems here, and are asked not to post too much code. If it's allowed (!), stickymail me all the code (inc HTML), or zip it to:

<snip>

[edited by: korkus2000 at 6:40 pm (utc) on Jan. 19, 2004]
[edit reason] No emails please [/edit]