Reputation: 10075
Please answer this question as I have failed to get proper understanding of the issue and simply cannot locate any material to read on it. The question, generically speaking, requires the consideration of the following commonly used structure. Yes, the JS may go at the bottom but that's a different discussion.
<html>
...
<script>
$(document).ready(function () {
.... //Script to be used in this document
});
</script>
...more html
<div id="ajax_div" ... >
<script>
**$(document).ready(function ()** { //2nd wrapper
....
});
</script>
... AJAX returned HTML
</div>
The question: Is the second wrapper required to be generated in the AJAX response? I understand that document.ready makes sure that the DOM is ready before the JS can work on it. Should the same logic, then, not apply to AJAX generated JS? Are there cases when it is required and when it is not required? I had a situation where the wrapper on-document-ready for AJAX generated JS fixed something but all along I have not been using it for other situations and all was well!!!
Hope its not a dumb question! If it is a repeat, please refer me to it. I could not locate any...
Upvotes: 3
Views: 1179
Reputation: 3669
Consider the following scenario:
Now, in step 2.3, it may happen that the returned code or the success/failure callbacks were capable of working with the partially loaded document since their targets were already loaded at that point of time. You'll then not notice any errors.
On the other hand, if the AJAX call itself is fired after the document is ready, then your generated JS doesn't have to wrap itself in a document.ready
block.
Upvotes: 1