Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$(document).ready(function(){ in JQuery... worry about overwriting?

Tags:

jquery

onload

In using this cool onload function from JQuery

$(document).ready(function(){ // my stuff }

do I need to worry about overwriting anything else that may have called it?

like image 828
Dan Rosenstark Avatar asked Aug 06 '09 16:08

Dan Rosenstark


People also ask

What does the jQuery $( document .ready function (){ statement do?

$( document ). ready() A page can't be manipulated safely until the document is "ready." jQuery detects this state of readiness for you. Code included inside $( document ). ready() will only run once the page Document Object Model (DOM) is ready for JavaScript code to execute.

What is replacement of $( document .ready function?

load(function(){ // ...}) @undefined, this is almost the same as $(document). ready(function(){ ... }) . load() will wait until the graphics are also loaded.

Can we use multiple document ready () function on the same page?

Yes we can do it as like I did in below example both the $(document). ready will get called, first come first served.


3 Answers

In jQuery, that function adds to the ready queue I believe, so you can write multiple ready() functions without worrying about overwriting previous ones (they just stack).

like image 56
OneNerd Avatar answered Sep 28 '22 03:09

OneNerd


@OneNerd is right in his answer that it just adds to the ready queue. The ready method is declared in jQuery like this:

ready: function( fn ) {
    // Attach the listeners
    jQuery.bindReady();

    // Add the callback
    readyList.add( fn );

    return this;
},

Ref: http://code.jquery.com/jquery.js

like image 38
awe Avatar answered Sep 29 '22 03:09

awe


$(document).ready is an event, so as many subscribers as you want can wire up to it.

like image 29
Tristan Warner-Smith Avatar answered Sep 30 '22 03:09

Tristan Warner-Smith