Hook a javascript event to page load

63,194

Solution 1

If you don't want to explicitly assign window.onload or use a framework, consider:

<script type="text/javascript">
function startClock(){
    //do onload work
}
if(window.addEventListener) {
    window.addEventListener('load',startClock,false); //W3C
} else {
    window.attachEvent('onload',startClock); //IE
}
</script>

http://www.quirksmode.org/js/events_advanced.html

Solution 2

Insert this anywhere in the body of the page:

<script type="text/javascript">
window.onload = function(){
    //do something here
}
</script>

Solution 3

The cleanest way is using a javascript framework like jQuery. In jQuery you could define the on-load function in the following way:

$(function() {
    // ...
});

Or, if you don't like the short $(); style:

$(document).ready(function() {
    // ...
});

Solution 4

window.addEventListener("load", function() {
    startClock();
});

This will invoke the startClock function at page load.

Share:
63,194

Related videos on Youtube

Jagd
Author by

Jagd

If there was a bronze badge for "Asked a dumb question", I'd have a whole lot of bronze badges.

Updated on July 09, 2022

Comments

  • Jagd
    Jagd almost 2 years

    I have an aspx that has the following javascript function being ran during the onload event of the body.

    <body onload="startClock();">
    

    However, I'm setting the aspx up to use a master page, so the body tag doesn't exist in the aspx anymore. How do I go about registering the startClock function to run when the page is hit and still have it use a masterpage?

  • Jason Bunting
    Jason Bunting almost 15 years
    +1 - I was too lazy to type this answer, but this is the way I would suggest someone does it too, with the same assumptions you initially state.
  • Jason Bunting
    Jason Bunting almost 15 years
    Of course, I would abstract the details a wee bit - hopefully that is obvious to the OP...
  • Jagd
    Jagd almost 15 years
    I must be missing something real obvious here, but where does the 'if' block of code go? You can't just dump that in a <script> tag can you?
  • Corbin March
    Corbin March almost 15 years
    You can stick it all in a script tag. I updated my answer to reflect it. This will force the 'if' logic to execute as the script is parsed so the startClock event is ready onload.
  • Jagd
    Jagd almost 15 years
    Easy and slick way to do it. Thanks.
  • Corbin March
    Corbin March almost 15 years
    Since non-MS browsers are pretty good about implementing W3C DOM Level 2 recommendations, the 'else' exists for IE only. As Jason mentioned, we could(should) abstract the details and while we're at it we could add caution(is this a browser that doesn't support either event reg. model, is this function already registered, etc). I opted for simplicity in my example. Worth a downvote? I dunno.
  • Jagd
    Jagd about 14 years
    Agreed. I've actually started using jQuery a few months after posting this question (nearly a year ago now), and I love it.
  • Doug Domeny
    Doug Domeny almost 13 years
    Newer versions of jQuery require the $(document).ready(...) style.
  • ThiefMaster
    ThiefMaster almost 13 years
    No. In never versions the behaviour of $() changed, but $(some_function) did not change.
  • Alan H.
    Alan H. almost 13 years
    I have seen code that tests to see if window.onload is defined, and if so, refers to it in a temp variable, then "calls" that variable from within the new window.onload.
  • fabb
    fabb almost 13 years
    In Chrome, document.addEventListener didn't work, but window.addEventListener did.
  • Philipp Michalski
    Philipp Michalski over 3 years
    I assume you mean RegisterClientScriptBlock?
  • JulienVan
    JulienVan about 2 years
    Nice, just don't forget to add ");" after the "}"
  • Kartikeya_M
    Kartikeya_M about 2 years
    Thanks for pointing that out @JulienVan. Have edited it.