开发者

Does alert('hello'); work in pageLoad() function?

Alert does not work in pageLoad, why? thanks

<html>
<head>  
    <scri开发者_运维知识库pt 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.


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>


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.


Try:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜