Synchronous – jfriend00 Oct 24 '11 at 02:46

  • @jfriend00 I think I worded the question incorrectly. I am using Ajax to dynamically and asynchronously load in scripts (see [here](http://stackoverflow.com/questions/7265371/how-to-properly-generate-exceptions-in-php-dojo-when-returning-javascript-code)). But that uses `eval` and debugging is a nightmare. So it was [suggested](http://stackoverflow.com/questions/7869957/is-there-any-way-other-than-to-use-eval-handleas-javascript-to-dynamically-f) I try using ` – puk Oct 24 '11 at 03:05
  • Yeah, I'm asking why you are loading them using Ajax? If you want sychronous, ` – jfriend00 Oct 24 '11 at 03:27
  • @jfriend00 sorry, the reason is that ` – puk Oct 24 '11 at 03:58
  • You're not understanding my question. If you use ` – jfriend00 Oct 24 '11 at 04:11
  • @jfriend00 the reason is that I need control. Script A might load scripts B , C , D. Furthermore, it might specify, for example, B be loaded first, then C and D. Script B then might load scripts B1, B2, then after both are loaded, load scripts B3-B10. Every script has a list like so `_pre = [[A] , [B , C] ,...]` so I load it, evaluate the list (`_pre`) and continue loading as necessery. But --and this answers your question-- I want the execution to be asynchronous (I just realized my wording was wrong). I don't want to load the scripts 1 at a time, but still want to know when they have loaded – puk Oct 24 '11 at 04:28
  • Here's how you can load scripts dynamically and know when they are loaded: http://unixpapa.com/js/dyna.html. – jfriend00 Oct 24 '11 at 04:30
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/4479/discussion-between-puk-and-jfriend00) – puk Oct 24 '11 at 06:12
  • 1 Answers1

    1

    One of the linked questions mentioned setting the innerHTML of the script tag element. I think this might do the trick

    function load(toEval, callback){
        __global_callback = callback;
    
        var node = document.createElement('script');
        node.innerHTML = toEval + '; __global_callback()';
        document.getElementsByTagName('head')[0].appendChild(node);
    }
    
    load("console.log('before...');", function(){
        console.log('...and after');
    });
    
    hugomg
    • 68,213
    • 24
    • 160
    • 246
    • If I understand correctly, you want me to get the script via Ajax, as I have done [here](http://stackoverflow.com/questions/7265371/how-to-properly-generate-exceptions-in-php-dojo-when-returning-javascript-code) (`handleAs: "text"` instead of `'javascript'`) then build the script from that. Essentially it's the same as ` – puk Oct 24 '11 at 04:04
    • Yes, that would be the idea (I don't know how robust or cross browser it would be though) – hugomg Oct 24 '11 at 11:56