How to Add an External Javascript File to a Web Page

Javascript

How to Add an External Javascript File to a Web Page

To make a web page link and show the contents of an external javascript file, inside the HTML head tag, you add a script tag to the HTML document and inside this opening script tag, you place the src attribute inside and have it set to the name of the external javascript file. The name of the javascript file must end in .js, signaling it is a javascript file. After this, you close the script tag.

Below is an example of a web page containing a reference to an external javascript file:


Note: When adding the src attribute to link to an external Javascript file, don't add any Javascript code between the opening and closing script tags. If you want to link to an external Javascript file and add more custom javascript code to a page, use a second set of script tags. Below is an example of this:


You can often and most likely often will attach multiple external Javascript files to a single web page. For example, one external Javascript file may be a reference to a drop-down navigation bar, while another allows you to add a slideshow of photos. On a page, you may want to have both of these features, so you would reference both external javascript files on that web page. In addition, as shown above, you can also add custom Javascript code to that single page, as needed. An example is shown below:




HTML Comment Box is loading comments...