Javascript - 评估一些加载外部 javascript 的 javascript 需要等待

发布于 2024-12-27 05:29:33 字数 2296 浏览 0 评论 0原文

我有一个问题。 我正在将 javascript 代码从数据库动态加载到 div 中(实际上可以通过任何 javascript 代码)。其中一个 JavaScript 是外部 JavaScript,另一个 JavaScript 包含对外部 JavaScript 的调用,这意味着我需要等待外部 JavaScript 加载才能调用该函数。

我的问题是我不知道函数名称,否则我会手动调用它们。 这是一个例子

<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>

我的主要问题是我不知道 addJS 中有什么。

我希望这足够具体以获得帮助,我将非常感激。 如果我可以提供更多信息,请告诉我。 另请注意,我没有在上面的示例中加载 jQuery,因此它不会在表中工作,这只是我想要完成的一个示例。 我尝试过的解决方案之一是使用 setTimeout 来执行此操作,方法是评估其上的每个部分并在每次交互之间等待。 (它也失败了)。

更新 好的,我已经创建了 2 个带有示例的工作链接 http://getryk.com/test.php (如果您等待最后几毫秒,您将获取 foo.bar 警报) http://getryk.com/test1.php(您不会收到 foo.bar 警报)

更新 01.17.2012 我有一个解决方案,我将每个脚本放入一个数组中,然后构建一个计时器来逐个运行它们(按照评论上的链接进行操作(不能在 script.onload 上放置超过 2 个超链接;() )。

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迷路的信 2025-01-03 05:29:33

如果没有任何 ajax 调用为什么不直接等待 document.ready 呢?这意味着页面已完全加载并且所有内容都在 DOM 对象内。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文