How To

How to Use Multiple JavaScript Files in Coordination with each other

Contributor
By eHow Contributing Writer
(2 Ratings)

JavaScript is a popular, powerful web scripting language, whose programs can be stored in files and can be used together. For instance, one file can include many frequently used functions and the other may actually call the functions in various orders. This article will describe how to use multiple JavaScript files in coordination with each other.

Difficulty: Moderately Challenging
Instructions

Things You'll Need:

  • computer
  1. Step 1

    Save your commonly used JavaScript code functions in a file with a .js extension. In this example, we will save them in a file called sample1.js:
    function myFunction() {
    alert('Hello world');
    }
    function yourFunction() {
    alert('Goodbye world');
    }

  2. Step 2

    Save your JavaScript code that actually calls these functions in another file, in this example sample2.js. Example:
    myFunction();

  3. Step 3

    Now in your HTML page, source both files in the proper order. All function definitions should come before the function calls. Example:






    My page




    This will print the myFunction alert window to your browser.

  4. Step 4

    Another page can reuse the sample1.js functions, but call them differently. Create sample3.js with the contents:
    yourFunction();

  5. Step 5

    In the second HTML page, your function file sample1.js is reused:






    Your page




    therefore reusing your functions and coordinating your JavaScript files.

Tips & Warnings
  • Short JavaScript routines can simply be added directly to the body of the HTML page instead of separate source files:
  • To hide JavaScript code from non-JavaScript enabled browsers, use the following syntax:
  • Be certain to keep up with all the latest CERT alerts regarding scripting vulnerabilities for JavaScript, Vbscript, and other scripting languages.
  • Many browsers and users have JavaScript disabled. Consider carefully whether a better-designed HTML page or another method would be more appropriate for your application than JavaScript.

Post a Comment

Post a Comment
  • Have you done this? Click here to let us know.
I Did This

Related Ads

Computers
Alexia Petrakos,

Meet Alexia Petrakos eHow's Computers Expert.

Get Free Computers Newsletters

Copyright © 1999-2009 eHow, Inc. Use of this web site constitutes acceptance of the eHow Terms of Use and Privacy Policy.   en-US

eHow Computers
eHow_eHow Technology and Electronics