13

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.

Nathan Arthur
  • 8,287
  • 7
  • 55
  • 80
Aaron Lee
  • 5,287
  • 5
  • 24
  • 19

10 Answers10

26

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);
Nathan Arthur
  • 8,287
  • 7
  • 55
  • 80
Matthew Crumley
  • 101,441
  • 24
  • 103
  • 129
7

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($);
kzh
  • 19,810
  • 13
  • 73
  • 97
  • 1
    This has been deprecated as of jQuery 1.8. See http://api.jquery.com/ready/ for more information. – nfplee Mar 05 '15 at 12:39
4

What if you tried building a module execution controller right into the ready function? Kind of like this:

https://gist.github.com/miguel-perez/476046a42d229251fec3

  • This was exactly what I needed. I have a single page app where the body of the page changes depending on the currently selected tab. Clicking on a new tab loads different content (html and javascript with a DataTables table). The other solutions on this page didn't help. – Exit42 Jul 07 '15 at 05:07
1

In some cases you need to add more code (scripts) in another places of the code, so creating a function limit you to write all the code in the same area. This why I prefer to add my custom events, and then trigger them:

//Add a page Ready custom event
$( document ).on( "page.ready", function( event ) {
    //Your Ready Scripts
});

//Add a page Load custom event
$( document ).on( "page.load", function( event ) {
    //Your Load Scripts
});

//Trigger Ready scripts on page ready
jQuery(function($) {
    $( document ).trigger( "page.ready" );
});

//Trigger Load scripts on page load
$(window).load(function () {
    $( document ).trigger( "page.load" );
});

//Trigger the Custom events manually
$( document ).trigger( "page.ready" );
$( document ).trigger( "page.load" );

https://learn.jquery.com/events/introduction-to-custom-events/

Roy Shoa
  • 3,117
  • 1
  • 37
  • 41
1

Aaron - what would you be trying to accomplish, reinitializing variables?

Edited... The beauty of jQuery in my opinion is the call back functions. Why not just have some action/function executed after the event.

RSolberg
  • 26,821
  • 23
  • 116
  • 160
0

I can see one scenario where this would be very useful, that is if you delay loading jQuery until everything else has loaded, then you dynamically append a new script element to load jQuery.

In this case, $(document).ready is not always triggered, since in many cases the document will have completely loaded before jQuery is done loading and has set up the right event handlers.

You could use named functions and call those instead, but it would still break any plugin that relies on $(document).ready.

Steadicat
  • 309
  • 2
  • 7
0

If it helps you:

var document_on_ready = new Array();

function on_ready(){
   for(i=0;i<document_on_ready.length;i++){
      document_on_ready[i].call();
   }
}
$(function(){
   on_ready();
});

var counter = 0;
document_on_ready.push(function(){
   counter++;
   console.log('step1: ' + counter);
});

document_on_ready.push(function(){
   counter++;
   console.log('step2: ' + counter);
});

window.setTimeout(on_ready,1000);
0

Easy: you just need to disable the safety-flag jQuery.isReady. Set jQuery.isReady to false and use jQuery(document).trigger('ready') to re-trigger this event. That's quite useful for turbolinks-style navigation.

Denis
  • 121
  • 2
  • 4
-1

ASP.Net UpdatePanel partial page loads are another place where one might want to re-trigger loading functionality - not that $().ready is the right place to do that. In that case, you can either use page_load or the MS framework's endrequest event.

Dan Davies Brackett
  • 9,811
  • 2
  • 32
  • 54
-2

You should be able to with document.ready.apply();

user64640
  • 93
  • 2