HTML Scripts

HTML Scripts or Javascript can be added using the element <script>, which makes a web page dynamic and interactive.

The element <script> can be used in two ways

  • Add Javascript directly to a web page
  • Link an external Javascript file to a web page

The element <script> can be added either inside <head> or <body> elements.

However, it's suggested to add scripts inside the <body> element before closing it.

Internal Javascript

Javascript is directly added to a web page using <script> element.

<script>
    document.getElementById("hello").innerHTML = "Hello JavaScript!!";
</script>

External Javascript

External Javascript file on the same server linked to a web page using its path

<script src="/assets/scripts/myscripts.js"></script>

External Javascript file on another server linked to a web page using its absolute path

<script src="https://randomcodez.com/html-examples/assets/scripts/myscripts.js"></script>

Example

<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
    <h1>HTML Scripts</h1>
    <p id="hello">Hello World!!</p>
    <p id="external">Hello User!!</p>
    <script>
        document.getElementById("hello").innerHTML = "Hello JavaScript!!";
    </script>
    <script src="/assets/scripts/myscripts.js"></script>
</body>
</html>

Overall

HTML element <script> can be used to add Javascript to an HTML document to make it dynamic and interactive.