Does Alert('Hello'); Work in pageLoad() Function?

Does Alert('Hello'); Work in pageLoad() Function?

Alert does not work in pageLoad, why? thanks

<html>
<head>  
    <script type="text/javascript">
        function pageLoad()
        {
            alert('hello');
        }
    </script>
</head>
<body />
</html>

Problem found: Dave Ward suggests that since my page does not have a script manager (which calls PageLoad for me). that is the reason I was puzzled. I never realised I had to call it for myself when there was no script manager.

5

4 Answers

Yes, but you need to invoke it somewhere:

<script type="text/javascript">

    function pageLoad()
    {
        alert('hello');
    }

    pageLoad();  // invoke pageLoad immediately

</script>

Or you can delay it until all content is loaded:

<script type="text/javascript">

    function pageLoad()
    {
        alert('hello');
    }

    window.onload = pageLoad;  // invoke pageLoad after all content is loaded

</script>
0

alternatively you can self invoke it

(function pageLoad() {
  alert('hello');
})();

pageLoad is never being called. Try the following:

<html>
<head>  
    <script type="text/javascript">
        function pageLoad()
        {
            alert('hello');
        }
        window.onload = pageLoad;
    </script>
</head>
<body />
</html>

Note a better way of doing this is by using jQuery and the following syntax:

$(window).load(pageLoad);

You could also use an alternative Javascript framework as most provide a similar way of doing this. They all take account of a number of issues related to attaching to event handlers.

3

Try:

<html>
<head>  
<script type="text/javascript">
    function pageLoad()
    {
        alert('hello');
    }
</script>
</head>
 <body onload="pageLoad()" />
 </html>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Marcus Vance
Author

Marcus Vance

Marcus Vance is a cybersecurity auditor and technology writer dedicated to educating the public about online safety, data privacy regulations, enterprise security, and emerging cyber threats.