HTML – Introduction to JavaScript

JavaScript Introduction:

  • JavaScript performs Client-side validations
  • JavaScript provides Functionality to web pages.
  • JavaScript executes with HTML code.
  • JavaScript is case-sensitive language

We can Write JavaScript code in places of HTML document

  1. In Body Location
  2. In Head Location
  3. External JS File

Body Location: Execute Script when web page is loading.

<!DOCTYPE html>
<html>
            <body>
                        <script>
                                    alert(“Alert box while loading page”);
                        </script>
            </body>
</html>

Head Location: Execute Script on action (for example click on button).

<!DOCTYPE html>
<html>
            <head>
                        <script>
                                    function run()
                                    {
                                                alert(“Alert when we click on button”);
                                    }
                        </script>
            </head>
            <body>
                        <button onclick=”run()”>Click Me</button>
            </body>
</html>

External JavaSctipt file:

web.html:

<!DOCTYPE html>
<html>
            <head>
                        <script type=”text/javascript” src=”mycode.js”></script> 
            </head>
            <body>
                        <button onclick=”alertMe()”>Click Me to Alert</button> <br/>
                        <button onclick=”promptMe()”>Click Me to Prompt</button>
            </body>
</html>

mycode.js:

function alertMe()
{
            alert(“It is alert box”);
}
 
function promptMe()
{
            var name = prompt(“Enter your name : “);
            alert(“Hello ” + name + “, Welcome”);
}
Scroll to Top