Java Script Lesson 8 ‘ Using More Than One JavaScript on a Page
JavaScript Lesson 8 ’ Using More Than One JavaScript on a Page
Now that you are leaning more about JavaScript, you may be wanting to add more than one script to your page. This can be done quite easily, allowing you to utilize all of the features that JavaScript can provide.
For example, if you wanted to add a JavaScript clock, slideshow and menu all on one page, it can seem complicated. However, there is an easy fix, which we will illustrate below.
First, you need to understand why conflicts can occur between two or more JavaScripts on the same page. Once you have this figured out, you can ensure that your scripts will function correctly, if you have 2 or 20 running on the same page.
Any conflict that could occur is usually caused by the ONLOAD event that you may see in some scripts. This event can be anything from a mouseover image, or a clickable button. When the event occurs, the script runs. So, how can you avoid this?
The ONLOAD event is normally located in the body tag.

Your scripts may call for different actions to be performed during the ONLOAD event. If you are experiencing this problem, here is a very easy fix.
Instead of trying to specify two different actions to be taken after the event separately, you reference them at the same time. For example:

Each script would be called at the same time, and they all need to be separated by a semicolon. If you only needed to run two scripts on the same page, you would just mention those two scripts at this point. If you have 20 scripts, you would need to add each one, with a semicolon in between.
Of course, you would need to replace the above words with the actions your script specifies, but this should help you get the general idea of what is needed.
Just remember, you will need to take these actions out of the scripts, if you have them located further down in your page. For example, if you have:

and later on:

You would need to reference the action for the second script in the body tag and remove that reference in the script below.
Our next lesson will cover how to add a JavaScript slideshow to your pages. If you have a collection of images you would like to display, this is a handy way to organize them!
Related Posts:Posted on 12/27/05 1:53 AM
Be the first to comment!