HTML JavaScript

Last Updated:

Adding JavaScript to HTML

There are two ways of adding JavaScript to your HTML. They are

  • External JavaScript
  • Internal JavaScript

External JavaScript

Use <script> tag with src attribute to link JavaScript to an HTML document.

Note: The external JavaScript file used in this example is alert-me.js

Example

HTML Online Editor
<!DOCTYPE html> <html lang="en-US"> <body> <button onclick="myFunction()">Click Me</button> <script src="/script/alert-me.js"></script> </body> </html>

Internal JavaScript

The HTML <script> tag is used to add internal JavaScript to an HTML document.

Example

HTML Online Editor
<!DOCTYPE html> <html lang="en-US"> <body> <button onclick="myFunction()">Click Me</button> <script> // Internal JavaScript goes here. function myFunction(){ alert("I'm from Internal JavaScript"); } </script> </body> </html>

Share this Page

Meet the Author