Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$(document).ready() not executed for ajax loaded content

Tags:

jquery

What could be reasons not to execute document.ready within returned partial page? It works fine 2 times, but on 3rd time nothing happens after update of html:

alert(html);
alert($(PopUpItem));
$(PopUpItem).html(html);
alert('in set popup html completed'); 

I have all alerts executed, PopupItem and html has correct values. I am using JQuery to execute server call by $.post.

Also, Can I have error handler to catch if some syntax error happened?

like image 954
st78 Avatar asked Feb 04 '10 14:02

st78


2 Answers

Within your returned AJAX, you shouldn't need a $(document).ready() call. The DOM has already been loaded. Something simple like this should do the trick:

<script type="text/javascript">
// Do something here.
</script>
like image 101
tambler Avatar answered Sep 20 '22 23:09

tambler


Are you saying that the html string passed to $(PopUpItem).html() contains a <script> block with a document.ready(function() { ... }); inside it?

If so the reason the ready event handler doesn't fire is that the script doesn't run at all. See this question which has the same ‘third-time’ behaviour.

Don't load <script> tags into markup. It's not at all reliable cross-browser, jQuery or no jQuery.

like image 21
bobince Avatar answered Sep 19 '22 23:09

bobince