HTML Functionality using JavaScript

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>

Join Our Channel

Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.

This channel is primarily useful for Full Stack Web Developer.

Share this Page

Meet the Author