Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to trigger $().ready() in jQuery?

Tags:

jquery

Using jQuery, is there a way to retrigger the document.ready trigger at some point after a page has loaded?

Update: jQuery sheds them once they are run. In my experiments it looks like jQuery will run a ().ready callback as soon as it is encountered once the initial ready event is triggered.

like image 490
Aaron Lee Avatar asked Feb 18 '09 18:02

Aaron Lee


People also ask

What is ready () function in jQuery?

jQuery ready() Method The ready event occurs when the DOM (document object model) has been loaded. Because this event occurs after the document is ready, it is a good place to have all other jQuery events and functions. Like in the example above. The ready() method specifies what happens when a ready event occurs.

Why do we start our code with document ready () in jQuery?

ready() function will load as soon as the DOM is loaded and before the page contents are loaded. You should wrap all your javascript code with this function to ensure that the code only runs when the page is fully rendered.

How do you call a function in document ready jQuery?

$( 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. Code included inside $( window ).

How do you rerun document ready?

You can't re-run document. ready as it only fires once per document. If you need this behaviour you should create a separate function which you can call on document. ready and then again at any later point to reset the state of the page.


2 Answers

You can do something like this:

$(document).on('ready readyAgain', function() {
    // do stuff
});

// At some other point in time, just trigger readyAgain
$(document).trigger('readyAgain');

Note:

$(document).on('ready') has been deprecated in jQuery 1.8 according to documentation. An alternative method is as follows:

function myReadyFunction(){}
$(myReadyFunction);

// at some other point, just call your function whenever needed:
myReadyFunction($);
like image 102
kzh Avatar answered Sep 20 '22 16:09

kzh


If you just need to make sure that new initialization functions get run, you don't need to do anything. When you add a new event handler in $().ready, after the document finishes loading, it will run immediately.

Apparently, calling $().trigger("ready") directly doesn't work because jQuery resets the ready event handlers after it runs them. You will need to keep track of the functions you need to call, and call them directly as below.

Of course, it might be better to just call the function directly, so you don't re-run something you didn't intend to (some plugins might have their own $().ready() functions that they don't expect to run twice).

$().ready(initializationFunction);

// later:
initializationFunction(jQuery);
like image 42
Matthew Crumley Avatar answered Sep 17 '22 16:09

Matthew Crumley