jquery 加载脚本

    技术2022-07-10  121

    jquery 加载脚本

    JavaScript loaders are incredibly powerful and useful utilities.  I've even covered a few of them on this blog, like curljs and LABjs, and have used RequireJS and the Dojo loader on personal projects.  They're super powerful but can be overkill in some cases.  If you're using jQuery, there's a built in method for loading a single script which may come in handy if you'd like to lazy load a plugin or any other type of script.  Here's how to use it!

    JavaScript加载器是功能强大且实用的工具。 我什至在本博客中介绍了其中的一些内容,例如curljs和LABjs ,并在个人项目上使用了RequireJS和Dojo加载器。 它们超级强大,但在某些情况下可能会过大。 如果您使用的是jQuery,则有一个用于加载单个脚本的内置方法,如果您想延迟加载插件或任何其他类型的脚本,该方法会派上用场。 这是使用方法!

    jQuery JavaScript (The jQuery JavaScript)

    jQuery is fitted with a getScript method to load one script; handling the result can be done in a few ways.  A basic usage of jQuery.getScript would look like:

    jQuery装有getScript方法来加载一个脚本。 处理结果可以通过几种方式完成。 jQuery.getScript的基本用法如下所示:

    jQuery.getScript("/path/to/myscript.js", function(data, status, jqxhr) { /* do something now that the script is loaded and code has been executed */ });

    The getScript method returns a jqxhr so you can also use it as a follows:

    getScript方法返回一个jqxhr,因此您也可以如下使用它:

    jQuery.getScript("/path/to/myscript.js") .done(function() { /* yay, all good, do something */ }) .fail(function() { /* boo, fall back to something else */ });

    The obvious use case for jQuery.getScript is lazy loading of a plugin and using it once loaded:

    jQuery.getScript的明显用例是延迟加载插件,并在加载后立即使用它:

    jQuery.getScript("jquery.cookie.js") .done(function() { jQuery.cookie("cookie_name", "value", { expires: 7 }); });

    If you need to do more advanced stuff like loading multiple scripts and different types of file types (text files, images, css files, etc), I'd recommend you switched to a JavaScript loader.  In the case of wanting to lazy load a plugin and not simply load it with each page, getScript is perfect!

    如果您需要做更多高级的事情,例如加载多个脚本和不同类型的文件类型(文本文件,图像,css文件等),建议您切换到JavaScript加载器。 如果要延迟加载插件而不是简单地在每个页面加载它, getScript是完美的!

    更新:缓存 (Update: Caching)

    It's important to note that when using jQuery.getScript, a timestamp is added to the script URL so the script ends up not being cached. Unfortunately you need to override all caching to cache the script and still use jQuery.getScript:

    重要的是要注意,使用jQuery.getScript , jQuery.getScript时间戳添加到脚本URL,因此脚本最终不会被缓存。 不幸的是,您需要重写所有缓存以缓存脚本,并且仍然使用jQuery.getScript :

    jQuery.ajaxSetup({ cache: true });

    If you don't want to override all caching with your AJAX requests, it's best the use the jQuery.ajax method with a dataType of script:

    如果您不想使用AJAX请求覆盖所有缓存,则最好将jQuery.ajax方法与dataType为script :

    jQuery.ajax({ url: "jquery.cookie.js", dataType: "script", cache: true }).done(function() { jQuery.cookie("cookie_name", "value", { expires: 7 }); });

    Keep caching in mind when loading your scripts!

    加载脚本时请注意缓存!

    翻译自: https://davidwalsh.name/loading-scripts-jquery

    jquery 加载脚本

    Processed: 0.012, SQL: 9