PDA

View Full Version : General Info question on two scripts on one page



Markxxx
02-22-2006, 06:21 AM
Not asking for help per rules. Just some infomration on what to look for

This is the question. I ran one script. Works fine.

I ran script two. Works fine

I put the two together and nothing. A conflict.

Both scripts use onload to run.

However if I change one script to onclick and leave the other as onload

As soon as I load the page, the first script runs and when I click anywhere, with the mouse, the second script runs fine.

So they both work fine in conjunction with one and other. Both run at the same time.

I realize that if you run two scripts sometimes you run into double names for event handlers and variables and functions. But if the scripts will work together with an onload and onclick, does this mean that that would be ruled out.

Again I am just curious about this? Seems to me if there was a conflict between the scripts they shouldn't work together at all.

Thanks for your help. You guys are great

jscheuer1
02-22-2006, 11:41 AM
Well, there are conflicts and there are conflicts. An interesting test you devised there for your two scripts and yes, as you describe it, it does demonstrate that the two particular scripts you used could work together if only they didn't conflict in grabbing the onload event of the page.

How to resolve this? It depends upon how each script is accessing the onload event of the page and also upon how you want to combine these two events into one.

The easiest way, if the two scripts cooperate, is to remove and/or move each scripts onload event to the body tag or leave it there and add to it if it is already there, say one is coded into the script like so:


window.onload=fillup

and the other already uses the body tag's onload event:


<body onload="goForIt();">

These two could be combined in the body tag like so:


<body onload="goForIt();fillup();">

The important thing to notice is that when coded in the script, an onload function call will look like so:

fillup

or

fillup;

But when placed in the body onload event must be made like so:

fillup();

More information is found following the tutorial in the sticky below:
Note: Questions regarding multiple script usage are not usually replied to due to their time consuming nature. See this <a href="http://www.dynamicdrive.com/forums/showthread.php?t=320">thread</a> for more information, and this <a href="http://www.javascriptkit.com/javatutors/multiplejava.shtml">tutorial</a> for one possible solution.

Markxxx
02-22-2006, 04:36 PM
Once again it works perfectly.

It took awhile to get it. I found it was easier to start over and cut and paste in sections. But it makes really good sense.

The links were great too.

Thank you for your help