简体   繁体   中英

Javascript - eval a couple of javascripts that load external javascript need to wait

I have a problem. I'm dynamically loading javascript codes into a div from a database (can by really any javascript code). one of the javascripts is an external javascript and another one after it contains a call to the external javascript meaning i need to wait for the external javascript to load before i can call the function.

My issue is that i don't know the functions name or i would call them manually. Here is an example

<html>
  <head>
  </head>
  <body>
    <div id='testdiv'> </div>

    <script type='text/javascript'>
      var mydiv = document.getElementById('testdiv');
      var addJS = '<script type="text/javascript">function testing() { alert("test"); }<\/script><script type="text/javascript">document.write("<script type=\'text/javascript\' src=external.js><\/script\>");<\/script><script type="text/javascript">call_too_external_function();<\/script>';

      addJS = addJS.replace(new RegExp('<\/script>', 'g'),"");
      addJS = addJS.replace(new RegExp('<script type="text/javascript">', 'g'),"");
      var tmp = document.write;
      document.write = function () {
        //catching the document write and evaling the content of the script
        //but still call_too_external_function is undefined..
        var justtest = [].concat.apply([], arguments).join('');
        var mysrc = jQuery(justtest).attr('src');
        jQuery("head").append(justtest);
        alert('need to wait!!!');
        //IF this alert exists and i wait a few milliseconds to click it call_too_external_function will work
        //IF i comment the alert out call_too_external_function will be undefined.
        //IS THERE A WAY TO PAUSE HERE untill this is loaded?
      }
      eval(addJS);
      document.write = tmp;
  </script>
</body>
</html>

My main problem is that i don't know what is in addJS.

I hope this is specific enough to get help i would really appreciate it. If there is any more information i can give please let me know. Please also notice i did not load jQuery on the above example so it will not work off the table its just an example of what i'm trying to accomplish. One of the solutions i've tried is doing this with setTimeout by eval'ing each section on its on and waiting between each interation. (it failed too).

UPDATE ok so i've created 2 working links with example http://getryk.com/test.php (if you wait for the last a few miliseconds you will get foo.bar alert) http://getryk.com/test1.php (you will not get foo.bar alert)

UPDATE 01.17.2012 I've got a solution i'm putting each script into an array then i've build a timer to run them one by one (followed the link on the comment (cannot put more than 2 hyperlinks ;()on the script.onload).

Thanks!

If there arent any ajax calls Why not just wait for document.ready? That would mean page is fully loaded and everything is within the DOM object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM