$(document).ready(handler) is executed after the DOM is fully loaded. If the content is later added to the page using AJAX, which contains the $(document).ready(handler) function, this function is executed immediately, in the jQuery API . In addition, .ready can only be called on a jQuery object corresponding to the current document.
However, this is not what I want :)
How can I achieve this type of .ready functions for data loaded via AJAX after .ready already running using a cross browser?
EDIT: Here's a very simplified example. I have a problem that I'm trying to solve, but I'm more interested in how to do it right.
Basically, the .ready function in ajaxPage.html is run before importantDependency.js is fully loaded, so the first load of ajaxPage, importantDependency is missing, but subsequent loads see it.
index.html
... <script type="text/javascript"> $(document).ready(function() { alert("The document is ready"); $('#myButton').click(function() { $('<div></div>').dialog({ open: function () { $(this).load('ajaxPage.html'); } }); }); }); </script> ...
ajaxPage.html
... <script type="text/javascript" src="importantDependency.js"></script> <script type="text/javascript"> $(document).ready() { $('#thing').leverageImportantDependency(); }); </script> ...
EDIT 2: I want to do this from the loaded content, and not from the page that calls the content. Changing the calling page means duplicating the code in each instance where it is called. I would like the behavior to be tied to the content, and not to its name.
source share