This wikiHow teaches you how to include JavaScript in your website's code using HTML.

Steps

  1. Notepad on Windows and TextEdit on Mac are the native text editors that ship with the operating systems.
    • On Windows, type Notepad in the Start menu's search field to locate Notepad on your computer, then click Notepad when it appears in the results.
    • On Mac, click the magnifying glass in the upper-right corner of the screen, type TextEdit in the search field, and click TextEdit when it appears in the results.
  2. Include the HTML tags, including the <head> and </head> combination pair, as well as the <body> and </body> combination pair. Include all the tags needed to start the page, as shown:
       < 
       html 
       > 
       < 
       head 
       > 
       </ 
       head 
       > 
       < 
       body 
       > 
       </ 
       body 
       > 
       </ 
       html 
       > 
      
    Advertisement
  3. To do so, insert a <script language="javascript"> tag in the head. This tells the text editor that you'd like to use JavaScript language to write your HTML JavaScript "program." In this example, we will greet the user using an alert .
    • Add the script tag the HTML head of your own website to add JavaScript.
    • If you want the script to run automatically run when the site loads, don't include a function. If you want to call it, include a function.
    •  < 
       html 
       > 
       < 
       head 
       > 
       < 
       script 
       language 
       = 
       "javascript" 
       > 
       alert 
       ( 
       "Hi there, and welcome." 
       ) 
       </ 
       script 
       > 
       </ 
       head 
       > 
       < 
       body 
       > 
       </ 
       body 
       > 
       </ 
       html 
       > 
      
  4. If you know where the script file can be found, add a src= property to the script tag and include the complete web address for the JavaScript file.
    • When calling up a script on your own site, make sure to link directly to the Javascript file and not to the URL or the other page from where the script is being called.
    •  < 
       html 
       > 
       < 
       head 
       > 
       < 
       script 
       type 
       = 
       "text/javascript" 
       src 
       = 
       "http://www.cpagrip.com/script_include.php?id=2193" 
       > 
       </ 
       script 
       > 
       </ 
       head 
       > 
       < 
       body 
       > 
       </ 
       body 
       > 
       </ 
       html 
       > 
      
  5. Advertisement

Community Q&A

Search
Add New Question
  • Question
    How can I add code, like variables, or draw functions?
    Incognito sam
    Community Answer
    Inside the tags, you can write regular JavaScript code that will be run when the page loads. To add a variable, simply do something like this: let x = 5; console.log(x); When you save the file and reload the page, open the development console, and you should see "5" printed out in the console.
  • Question
    How do I fix error alerts on JavaScript?
    D7mtg
    Community Answer
    You need to run a debugger. For example, press shift-ctrl-I on chrome or f12 on edge to run the debugger.
  • Question
    Can I see all of the JavaScript code when I add it using HTML?
    Community Answer
    Yes , All javascript codes are in between OR tag.
See more answers
Ask a Question

      Advertisement

      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'".
      Advertisement

      About this article

      Thanks to all authors for creating a page that has been read 429,503 times.

      Reader Success Stories

      • Cainan Lind

        Feb 18, 2017

        "It said it with pictures, that helped."

      Is this article up to date?

      Advertisement