在< script>之后加载jQuery.load()吗?内容加载? [英] Does jQuery.load() load after <script> content is loaded?

查看:74
本文介绍了在< script>之后加载jQuery.load()吗?内容加载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

两个问题:


  1. jQuery.load()完全下载< script> 的内容后运行?

  1. Does jQuery.load() run after the content of <script> is completely downloaded?

如果有以下情况,则有< script> 在文档中将动态注入另一个< script> load()在下载第二个脚本的内容之后运行,还是在加载原始的非动态内容后运行?

If in case, there is a <script> in the document that will inject another <script> dynamically, does load() run after after the content of the 2nd script is downloaded or it will run after the original, non-dynamic content is loaded?

谢谢

2)问题的例子就像那:

The example for the 2) question is like that:

<html>
<head>
<script src="myscrip1.js"></script>
<script>
$(document).load( myscript2.functionA );
</script>
</head>
</html>

myscript.js会将myscript2.js注入dom。
,其中myscript2.js包含函数myscript2.functionA

Where myscript.js will inject myscript2.js into the dom. in which myscript2.js include the function myscript2.functionA

显然,我想在myscript2.js完全加载后运行myscript2.functionA。

Obviously, I want to run myscript2.functionA after myscript2.js is loaded completely.

:)

推荐答案

所有资源都会触发文档就绪事件已经下载了初始HTML中引用的(或者在出现错误时超时)。如果您动态地将引用注入另一个脚本(例如facebook api,google analytics等),那么与文档就绪事件相关的准备就绪是未定义的。

The document ready event is fired when all of the resources referenced in the initial HTML have been downloaded (or timed out in case there are errors). If you dynamically inject a reference to another script (like the facebook api, google analytics, etc) it's readiness is undefined with relation to the document ready event.

如果要检查外部脚本是否准备就绪,可以检查它创建的对象是否有已加载

If you want to check that your external script is ready you can check that an object that it creates has been loaded.

<script type="text/javascript">
  var startAfterJqueryLoaded = function(){
    if(typeof jQuery === "undefined" ) {
        setTimeout( startAfterJqueryLoaded, 100 );
        return;
    }

    // jQuery is ready, do something
  }

  startAfterJqueryLoaded();

</script> 

或者如果您控制了动态注入的脚本,您可以建立一个全局函数当它准备好时打电话。

Or if you have control of the script you are dynamically injecting you can establish a global function that it will call when it's ready.

<script type="text/javascript">
  window.dynamicScriptIsReady = function(){
    // do something
  }
</script> 


// Dynamic.js
// ...Setup whatever

window.dynamicScriptIsReady();

这篇关于在&lt; script&gt;之后加载jQuery.load()吗?内容加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆