Inserting JavaScript into Your Webpage

By: Dusty Arlia
Published on August 1, 2012
Last Updated on Saturday, July 11, 2015 at 5:00 PM
Total Updates: 9

Web browsers expect HTML when displaying your webpage. In order to use JavaScript you must use the script tag (<script>) to let the browser know that a script is coming. To specify the type of script, use the type attribute like so:

<script type="text/javascript" src="yourExtJs.js"></script>

This lets the browser know that you are using a JavaScript type script.

You can reference an external JavaScript file like in the example above or you can insert the JavaScript directly into your HTML document. Just place your JavaScript program between the opening script tag (<script>) and the closing script tag (</script>).

You can place your JavaScript program anywhere on your webpage. A lot of the time you'll see the <script> tag in the <head> portion of your webpage. This is done to make locating the script easier. Also, JavaScript will load before the HTML will when the JavaScript is located in the <head> section.

Sometimes you will need to put your JavaScript program right before the closing </body> tag. By doing this you assure the page is loaded and the visitor sees all the HTML before running your JavaScript. The timing in which you want your JavaScript to load may vary.

You are able to include and reference multiple scripts on a single HTML document. How this usually works is that you will place one or two JavaScript libraries in the <head> section of your webpage by referencing an external JavaScript file for each library and then include some JavaScript within the <body> section of your webpage. Using external JavaScript files is usually preferred for reusablity reasons.