Patrick
Patrick

Reputation: 73

i want to call a jquery function on load of the page

I want to call a jquery function on load of the page. There is also another Javascript getting called onload of the body tag.

Upvotes: 2

Views: 25015

Answers (3)

jmar777
jmar777

Reputation: 39649

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.

Upvotes: 8

sgriffinusa
sgriffinusa

Reputation: 4221

There is a jQquery handler called .ready() that will do what you want. It executes with the DOM is ready. See also Use Onload or ready? for a discussion about the differences between ready and onload.

Upvotes: 3

Nick Craver
Nick Craver

Reputation: 630349

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.

Upvotes: 12

Related Questions