JavaScript for Beginners - External JavaScript (P: III)
External
JavaScript
Scripts can also be placed in external
files. External scripts are useful and practical when the same code is used in
a number of different web pages
JavaScript file have the file extension .js
Below we’ve created a new text file, and
called it as customScript.js
To use an external script, put the name of
the script file in the src (source) attribute of the <script> tag.
Here is an example:
Your customScript.js file includes the
following JavaScript:
The result of the above example will be
identical to the result when we included the JavaScript within the HTML file
You can place an external script reference
in <head> or <body>, whichever you prefer. The script will behave
as if it were located exactly where the <script> tag is located.
Note: External scripts cannot contain <scripts> tags.
Note: Placing a JavaScript in an external file has the
following advantages
- It separates HTML and code
- It makes HTML and JavaScript easier to read and maintain.
- Cached JavaScript files can speed up page loads.
Comments
Post a Comment