javascript - doc.ready() and parseHtml -


so seems when retrieving html via ajax , inserting page using .html(parsehtml(result,true));, javascript inside result executed without regard doc.ready.

i'm guessing because parent page "ready" , there no concept/implementation of ready event html fragments+script being inserted page via parsehtml.

i tested so. you'd expect output 1, 3, 2, because doc ready occur last, output 1,2,3. purely coincidence though.

console.log('1');  $(document).ready(function () {     console.log('2'); }); console.log('3'); 

some code not working in doc.ready initializing tinymce editor, works fine if put 500ms delay on fake wait doc.ready. seems issue javascript running before new html integrated dom. settimeout hack won't work consistently.

so question is:

does .html($.parsehtml respect doc.ready inside result, in waits new html ready in dom running script? there other way include script inside ajax html result , have wait run until added html "ready" in dom?

from documentation

if .ready() called after dom has been initialized, new handler passed in executed immediately.

note refers dom in general. when you're loading additional elements later using ajax, dom still in initialized state, above applies.

if use .load() load html contains <script> elements, believe scripts executed after other elements have been added dom. there's no need use $(document).ready() in these scripts, there should no harm in doing so.


Comments

Popular posts from this blog

c++ - CryptStringToBinary API behavior -

java.util.scanner - How to read and add only numbers to array from a text file -

iphone - Three second countdown in cocos2d -