Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
JavaScript Onload Techniques
<script type="text/javascript">
/**
* Several different ways to calls an onload event
* first up, here's a function we will call using all the methods
*/
function artlungOnload() {
alert('The body of the document has loaded');
}
</script>
<!-- onload event handler attribute on the [body] tag -->
<body onload="artlungOnload()">
</body>
<!-- set the onload attribute equal to a function using a [script] tag -->
<script type="text/javascript">
window.onload = artlungOnload
</script>
<!-- set the onload attribute equal to an anonymous function that calls your load function -->
<script type="text/javascript">
window.onload = function() {
// your function calls here
artlungOnload();
}
</script>
<!-- in dojo, you use the addOnLoad() event to add function calls -->
<script type="text/javascript">
// Dojo
dojo.addOnLoad(function() {
// your function calls here
artlungOnload();
});
</script>
<!-- in ExtJS, you call the onReady() method and place function calls inside that -->
<script type="text/javascript">
// ExtJS
Ext.onReady(function() {
// your function calls here
artlungOnload();
});
</script>
<!-- in Glow and JQuery, call the ready() method -->
<script type="text/javascript">
// Glow
glow.ready(function() {
// your function calls here
artlungOnload();
});
</script>
<script type="text/javascript">
// jQuery
jQuery(document).ready(function(){
// your function calls here
artlungOnload();
});
// idiomatic jQuery allows you to use the alias $ for jQuery, so this is also acceptable
$(document).ready(function(){
// your function calls here
artlungOnload();
});
</script>
<!-- MooTools allows you to add function calls to the "domready" event -->
<script type="text/javascript">
// Mootools
window.addEvent('domready', function() {
// your function calls here
artlungOnload();
});
</script>
<!-- Prototype observe()'s the load event -->
<script type="text/javascript">
// Prototype
Event.observe(window, 'load', function(){
// your function calls here
artlungOnload();
});
</script>
<!-- See other examples at http://lab.artlung.com/rosetta/ -->
<!-- Send other suggested ways to add function calls onload to joe@artlung.com -->
<!-- follow me on twitter at http://twitter.com/artlung -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.