jquery - Wait for complete execution of javascript code in an injected <script> element -


i'm loading javascript code via ajax (as <script> block inside html string), , i'm using jquery.html() inject payload <div>. jquery.html() method automatically evals injected <script> elements.

i wait code in <script> block complete execution before releasing execution main event loop. if <script> contains callbacks, $.ajax().done() or setinterval(). if causes infinite loop, , if blocks other events being processed long time.

is possible? i'm wondering if there's kind of synchronization-wrapper browser-supported utility in javascript. hunch no, since seems contrary single event-loop nature of javascript, thought i'd check :) ultimately, goal insert custom javascript page upon specific user action, , guarantee code executed in entirety.

note don't need use jquery.html() or <script> injection, that's starting point. maybe best can tell client invoke specific pre-defined callback function when code finished, , make sure don't change window until function invoked?

you cannot "wait code in block complete execution before releasing execution main event loop" in javascript. cannot done.

done right, insert script tags onload handlers notified (via callback) when scripts had finished loading , finished synchronous execution (but not async execution).

if wanted know when async execution in scripts done, have build scripts notify when done.

if you're going use jquery's .html() script tag injection lose lot of control on process , becomes hacking project try figure out can outside process.

in cases, cannot prevent continuing execution of main event loop of javascript while waiting finish.


Popular posts from this blog