CBSE 21. WEB SCRIPTING - JAVA SCRIPT (5 MARKS)

 

29. Execute JavaScript Immediately After Page Load

  • Using the <script> Tag:
    • The most common method is to place your JavaScript code directly within the <script> tag.
    • The browser will execute this code as it parses the HTML.

HTML

<!DOCTYPE html>

<html>

<head>

    <title>Page Load Example</title>

</head>

<body>

 

    <h1>Welcome to the Page!</h1>

 

    <script>

        alert("Page has loaded!");

        console.log("This message appears in the browser console.");

    </script>

 

</body>

</html>

  • Using the onload Event:
    • Attach the JavaScript code to the onload event of the <body> tag.

HTML

<!DOCTYPE html>

<html>

<head>

    <title>Page Load Example</title>

</head>

<body onload="myFunction()">

 

    <h1>Welcome to the Page!</h1>

<script>

        function myFunction() {

            alert("Page has loaded!");

            console.log("This message appears in the browser console.");

        }

    </script>

 

</body>

</html>


No comments:

Post a Comment