Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trigger $document.ready (so AJAX code I can't modify is executed)

My requirements are the following:

  • I've got a rich webpage that at a certain moment loads a bunch of HTML in a div, via AJAX.
  • The HTML I retrieve does have javascript (<script>...</script>)
  • The retrieved javascript contains $('document').ready( ... ) parts
  • I can not modify the retrieved javascript; it comes from an external lib
  • I've got a javascript function that is called when the AJAX is loaded. I'm trying to "trick it" into executing by doing:

    function AjaxLoaded() {   $('document').trigger('ready'); } 

That doesn't cut it, I'm afraid.

I've seen several responses on Stack Overflow that "evade" this question by changing the code that is returned on the AJAX (make it a function and call it after loading, or just remove the $(document).ready()). I need to stress out that I can't change the retrieved code on this case.

like image 555
kikito Avatar asked Feb 10 '10 15:02

kikito


People also ask

How can I call document ready function after Ajax call?

It would turn into: function OnloadFunction () { alert("test"); } $(document). ready(OnloadFunction); Then you can call OnloadFunction whenever you want to.

What does $( document .ready function () 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 $( document .ready () and $( window .load () in jQuery?

ready() and $(window). load() event is that the code included inside onload function will run once the entire page(images, iframes, stylesheets,etc) are loaded whereas the $(document). ready() event fires before all images,iframes etc. are loaded, but after the whole DOM itself is ready.

What is the benefit of using document ready in jQuery?

jQuery document ready is used to initialize jQuery/JavaScript code after the DOM is ready, and is used most times when working with jQuery. The Javascript/jQuery code inside the $(document). ready() function will load after the DOM is loaded, yet before the page contents load.


1 Answers

Afer some research i created a way to get it to work.

here is my test that shows it working: http://www.antiyes.com/test/test2.php

here is the relevant code:

<script>     // easy copy of an array     Array.prototype.copy = function() {         return [].concat(this);     };      // this function is added to jQuery, it allows access to the readylist     // it works for jQuery 1.3.2, it might break on future versions     $.getReadyList = function() {         if(this.readyList != null)             this.myreadylist =  this.readyList.copy();               return this.myreadylist;     };      $(document).ready(function() {         alert("blah");     });  </script>  <script>      // this should be added last so it gets all the ready event     $(document).ready(function() {         readylist = $.getReadyList();     });  </script> 

then in the body I have:

<input type="button" onclick="$(readylist).each(function(){this();});" value="trigger ready" /> 

basically what i did was add a function to jQuery that copies the readyList before it's cleared out, then it will be available to be used by you.

it looks like the code below doesnt work:

function AjaxLoaded() {     $(document).trigger('ready'); } 

drop the quotes around document.

like image 185
John Boker Avatar answered Sep 18 '22 16:09

John Boker