Use jQuery to load script

Source: Internet
Author: User
Tags getscript

Translated by: Tie

Translated on: February 1, January 9, 2014

Original Article Date: January 1, January 6, 2014

Original article:Loading Scripts with jQuery

The JavaScript loaders loader is simple, powerful, and very useful. I have introduced some of them in my blog, such as curljs and LABjs. I also use RequireJS and Dojo to load programs in my project. They are so powerful, but in some cases it may be a cow. If you are using jQuery, you will find that jQuery has built-in methods to load a single script. If you want to delay loading plug-ins or other types of scripts, This method may be used. The following describes how to use it!

The jQuery JavaScript
JQuery provides the getScript method to load scripts. You can perform result processing in multiple ways. The basic usage of jQuery. getScript is as follows:
JQuery. getScript ("/path/to/myscript. js ", function (data, status, jqxhr) {/* after the script is loaded and executed, You can execute some special processing here. */});
The callback function of the getScript method provides a jqxhr parameter. Of course, it can be processed as follows:
JQuery. getScript ("/path/to/myscript. js "). done (function () {/* loaded successfully */}). fail (function () {/* loading failed, fall back processing */});
The most common use case of jQuery. getScript is to delay loading a plug-in and use it after loading:
jQuery.getScript("jquery.cookie.js")    .done(function() {        jQuery.cookie("cookie_name", "value", { expires: 7 });});
If you need more advanced loading functions, such as loading multiple scripts at the same time or loading different types of files (text, images, css, etc.), you should change to a JavaScript loader.
Sometimes $. getScript is a good solution to delay loading a plug-in after certain conditions are met.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.