Tuesday, June 4, 2024
 Popular · Latest · Hot · Upcoming
71
rated 0 times [  75] [ 4]  / answers: 1 / hits: 26814  / 15 Years ago, wed, february 10, 2010, 12:00:00

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.


More From » jquery

 Answers
32

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.


[#97610] Monday, February 8, 2010, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
robertjaysona

Total Points: 276
Total Questions: 110
Total Answers: 116

Location: Finland
Member since Sat, Nov 6, 2021
3 Years ago
robertjaysona questions
;