| Viewed 106 times

how to use javascript in html


how to use javascript in html

1 Answer
Mikdam Qandeel
Answered
1

Javascript in HTML pages makes them dynamic and interactive.

We can define the javascript code with <script> tag. and adding it inside the <head> section, Inside the <body> section or even after the </body> tag, it depends on, when you want to load the javascript code.

Example:

<!DOCTYPE html>
<html>

    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>Today's Date</title>

        <script>
            alert("Hello JavaScript!");
        </script>

    </head>
    <body>
    </body>
</html>

 

Try this code and reload the HTML page, it will display Hello JavaScript!.

On the other hand, we can Separate JavaScript File It's like CSS Stylesheet but we use .js extension for javascript file.

And separating the javascript file it makes the page maintenance easier and when JavaScript files are cached, pages load more quickly.