Can I call $(document).ready() to re-activate all on load event handlers?

I did something like:

// When document is ready...
$(function(){
    onPageLoad();
});

function onPageLoad(){
  // All commands here
}

Now I can call this function anytime I need.


Since you asked how to do it without modifying the external JS files, I'll answer that way. I've traced through the .ready() function in jQuery in the debugger and it appears that the root function that gets called when the page is ready is this:

jQuery.ready();

But, it appears you cannot just call it again to accomplish what you want because it appears that when it fires the first time, it unbinds from the functions that were previously registered (e.g. forgetting them). As such, calling jQuery.ready() manually a second time does not retrigger the same function calls again and I verified that in the debugger (breakpoint was only hit once, not second time).

So, it appears that you cannot solve this problem without either changing the jQuery implementation so it doesn't unbind (to allow multiple firings) or changing each piece of ready handler code to use your own events that you can fire as many times as you want.


A simple way to achieve this is just to invent your own event like this:

$(document).bind('_page_ready', function() { /* do your stuff here */});

Then add this:

$(function() { $(document).fire('_page_ready'); }); // shorthand for document.ready

And last, whenever you need to run it again you simply call this:

$(document).fire('_page_ready');

[Edit]

If you really can't edit the external script-files I've made a jsFiddle that makes what you want to do possible, you can take a look at the code here: http://jsfiddle.net/5dRxh/

However, if you wan't to use this, it's important that you add this script RIGHT AFTER you include jQuery, like this:

<script src="jquery.js" type="text/javascript"></script>
<script>
    //script from jsFiddle (only the plugin part at the top).
</script>
<!-- All the other script-files you want to include. -->