40
readers helped!

This helped me

How to Add JavaScript to Your Website Using HTML

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

Steps

  1. How.com.vn English: Step 1 Open a simple text editor.
    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. How.com.vn English: Step 2 Start an HTML block.
    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>
  3. How.com.vn English: Step 3 Add a script tag to the HTML head.
    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. How.com.vn English: Step 4 Call up other JavaScript scripts using a JavaScript function.
    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. How.com.vn English: Step 5 Click File in the menu bar, and Save As… (Windows) or Save (Mac).
  6. How.com.vn English: Step 6 Click .html in the format drop-down.
  7. How.com.vn English: Step 7 Name your document and click Save.
  8. How.com.vn English: Step 8 Double-click your document to open it in a browser.
    Advertisement

Community Q&A

Search
Add New Question
  • Question
    How can I add code, like variables, or draw functions?
    How.com.vn English: Incognito sam
    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?
    How.com.vn English: D7mtg
    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?
    How.com.vn English: Community Answer
    Community Answer
    Yes , All javascript codes are in between OR tag.
See more answers
Ask a Question
200 characters left
Include your email address to get a message when this question is answered.
Submit

      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

      How.com.vn English: How.com.vn Staff
      Co-authored by:
      How.com.vn Staff Writer
      This article was co-authored by How.com.vn Staff. Our trained team of editors and researchers validate articles for accuracy and comprehensiveness. How.com.vn's Content Management Team carefully monitors the work from our editorial staff to ensure that each article is backed by trusted research and meets our high quality standards. This article has been viewed 429,368 times.
      How helpful is this?
      Co-authors: 29
      Updated: June 3, 2021
      Views: 429,368
      Thanks to all authors for creating a page that has been read 429,368 times.

      Reader Success Stories

      • How.com.vn English: Cainan Lind

        Cainan Lind

        Feb 18, 2017

        "It said it with pictures, that helped."

      Is this article up to date?

      ⚠️ Disclaimer:

      Content from Wiki How English language website. Text is available under the Creative Commons Attribution-Share Alike License; additional terms may apply.
      Wiki How does not encourage the violation of any laws, and cannot be responsible for any violations of such laws, should you link to this domain, or use, reproduce, or republish the information contained herein.

      Notices:
      • - A few of these subjects are frequently censored by educational, governmental, corporate, parental and other filtering schemes.
      • - Some articles may contain names, images, artworks or descriptions of events that some cultures restrict access to
      • - Please note: Wiki How does not give you opinion about the law, or advice about medical. If you need specific advice (for example, medical, legal, financial or risk management), please seek a professional who is licensed or knowledgeable in that area.
      • - Readers should not judge the importance of topics based on their coverage on Wiki How, nor think a topic is important just because it is the subject of a Wiki article.

      Advertisement