Hook a javascript event to page load
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.
Related videos on Youtube
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, 2022Comments
-
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 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 almost 15 yearsOf course, I would abstract the details a wee bit - hopefully that is obvious to the OP...
-
Jagd almost 15 yearsI 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 almost 15 yearsYou 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 almost 15 yearsEasy and slick way to do it. Thanks.
-
Corbin March almost 15 yearsSince 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 about 14 yearsAgreed. I've actually started using jQuery a few months after posting this question (nearly a year ago now), and I love it.
-
Doug Domeny almost 13 yearsNewer versions of jQuery require the $(document).ready(...) style.
-
ThiefMaster almost 13 yearsNo. In never versions the behaviour of
$()
changed, but$(some_function)
did not change. -
Alan H. almost 13 yearsI 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 newwindow.onload
. -
fabb almost 13 yearsIn Chrome,
document.addEventListener
didn't work, butwindow.addEventListener
did. -
Philipp Michalski over 3 yearsI assume you mean RegisterClientScriptBlock?
-
JulienVan about 2 yearsNice, just don't forget to add ");" after the "}"
-
Kartikeya_M about 2 yearsThanks for pointing that out @JulienVan. Have edited it.