I want to call a jquery function on load of the page. There is also another Javascript getting called onload of the body tag.
+4
A:
Here's some good introductory reading on the subject, by the way.
No problem - they won't conflict with each other:
$(window).load(function() {...});
or...
$(document).ready(function() {...});
Use the second one if you don't need to wait for images and other external dependencies. It simply waits for the DOM to be "ready" (i.e., completely constructed).
Here's some good introductory reading on the subject, by the way.
jmar777
2010-08-10 19:16:14
The first should be `window`, it has the `onload` event :)
Nick Craver
2010-08-10 19:17:50
Not so... try it.
jmar777
2010-08-10 19:18:38
... guess I should add that, yes, the window object of course had the load event. The jQuery convention though seems to be to use the load pseudo-event on the document object. So... you're right, but my code will still work :)
jmar777
2010-08-10 19:20:03
@jmar777 - It won't...here's a test to show it :) http://jsfiddle.net/nick_craver/8XYpq/
Nick Craver
2010-08-10 19:20:56
Woooooooow.... /eats-foot /updates-example /+1
jmar777
2010-08-10 19:35:41
+1 for a correct answer :)
Nick Craver
2010-08-10 19:39:45
+7
A:
You can do it like this:
$(function() {
//do something
});
Or if you already have the function, like this:
function myFunction() {
//do something
}
You can call it like this:
$(myFunction);
Both of the above are equivalent to $(document).ready(function);, they're just shortcuts.
Nick Craver
2010-08-10 19:17:02
+3
A:
There is a jQquery handler called .ready() that will do what you want. It executes with the DOM is ready. See also http://stackoverflow.com/questions/3197942/use-onload-or-ready for a discussion about the differences between ready and onload.
sgriffinusa
2010-08-10 19:17:37