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.
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.
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.
$( 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 ).
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.
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');
$(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($);
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With