Add JavaScript to Your Website Using HTML

Here we will show you how to include JavaScript on your website. With a few rather simple additions, you can include this in your HTML page. Read on from Step 1 below to find out how this can be done

Steps

  1. Open up the Notepad or other simple text-editing software. Use the Run Command (Windows key+R), then type notepad and press Enter. It can also be found in Start menu > Accessories > Notepad.
  2. Start a basic HTML document. Include the HTML tags, including the <head> and </head> combination pair as well as the <body> and </body> combination pair. Type all the needed tags to start the page. It should look like this or the below image:
  3. Add a script tag to the HTML's head, and let the text editor know that you'd like to use a javascript language to write your HTML's javascript "program". Add a <script language="javascript"> tag in the head. If you want the script to automatically run when the site loads, don't do a function. If you want to call it, do a function. For this example, we will alert the user.
  4. Call up other JavaScript scripts using a JavaScript function, if you know where the script file can be found. Add it a src (stands for "src") property to the script tag and include the complete web address of the file. Make sure to link directly to the Javascript file and not the page's URL where the script is being called from by the other page's creator.
  5. Make sure to end your HTML document. Following the </body> tag, include the </html> tag.
  6. Save your page. Save the page as (anything).html, it will become a web page. Double click on the icon, and watch as your page loads and an alert box comes up.

Warnings

  • You have to determine the tag's MIME type in HTML4. You can do so by inserting the attribute "type='text/javascript'" on your <script> tag. You do not have to determine the type attribute in HTML5.
  • In HTML3, instead of "type='text/javascript'", you'll need to insert "language='javascript'".

Related Articles

You may like