tags:

views:

60

answers:

3

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: 

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
The first should be `window`, it has the `onload` event :)
Nick Craver
Not so... try it.
jmar777
... 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
@jmar777 - It won't...here's a test to show it :) http://jsfiddle.net/nick_craver/8XYpq/
Nick Craver
Woooooooow.... /eats-foot /updates-example /+1
jmar777
+1 for a correct answer :)
Nick Craver
+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
+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