0

I have a simple question but its haunting me since many days and i couldnt find the solution. I would like to fire a javascript event for everytime the page is loaded or rendered.

can any body please help.

chaosguru
  • 1,821
  • 4
  • 29
  • 42

4 Answers4

5

you can use <BODY onLoad="alert('hello world!')">

See some drawbacks and workaround on this thread: Is there a cross-browser onload event when clicking the back button?

[EDIT] or better (?), use:

window.onload=function() { 
   alert ('hello world');
}; 

(from this thread)

Community
  • 1
  • 1
JMax
  • 25,301
  • 12
  • 66
  • 87
4

Try using this:

<html>
    <head>
        <script type="text/javascript">

            function function_name() {
               alert('loaded');
            }

        </script>
    </head>

    <body onload="function_name()">
        <p>hello</p>
    </body>

</html>

Although the best way would probably be to use jQuery's ready() function to ensure browser compatibility.

Arj
  • 1,879
  • 3
  • 23
  • 45
3
window.onload = function(){/*your code*/}
ravi404
  • 6,949
  • 4
  • 29
  • 39
0

if your reqirement is such that the script needs to execute after the page has loaded completely you could write it in the following way

$(window).bind('load',function(){
    //your code here
});

document.ready works when the page html elements are loaded... the above code executes after the entire page (with its styling and images etc.) hence i believe that this requires a seperate mention

optimusprime619
  • 744
  • 2
  • 17
  • 37