Requirejs was created by James Burke, who is also the founder of the AMD specification.
Requirejs will let you write JavaScript in a different way than usual. You will no longer use the script tag to introduce JS files in HTML and to manage dependencies without the script tag order.
Of course, there will be no blocking (blocking) situation happening. OK, start with a simple example.
Create a new directory with the following structure
There are index.html, Jquery-1.7.2.js, Main.js and require.js under directory R1. Require.js and Jquery-1.7.2.js to their respective official website to download.
Index.html as follows
| 1234567891011 |
<!doctype html> <title>requirejs入门(一)</title> <meta charset="utf-8"> <script data-main="main"src="require.js"></script> <body> </body> |
Using Requirejs is simple, just introduce it through the script tag in the head (in fact, except for Require.js, no other file modules are introduced with the script tag).
The careful classmate will find a more custom attribute on the script label: data-main= "Main", the main.js to the right of the equal sign. Of course, you can use any name. This main refers to the main module or the Portal module, like the main function of C or Java main.
Main.js as follows
| 123456789 |
require.config ({ &NBSP;&NBSP;&NBSP;&NBSP; paths: { &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; JQUERY:&NBSP; ' jquery-1.7.2 ' &NBSP;&NBSP;&NBSP;&NBSP; } }); require ([ ' jquery ' ],&NBSP; function ($) { &NBSP;&NBSP;&NBSP;&NBSP; alert ($ (). jquery); |
Main.js calls Require.config and require in two functions.
Require.config is used to configure some parameters that will affect some of the behavior of the Requirejs library.
Require.config parameter is a JS object, commonly used configuration has baseurl,paths and so on.
The paths parameter is configured here, using the module name "jquery", whose actual file path jquery-1.7.2.js (suffix. js can be omitted).
We know that jquery has been supporting the AMD specification since 1.7, that is, if jquery is running as an AMD module, its module name is "jquery". Note that "jquery" is fixed and cannot be written "jquery" or anything else.
Note: If you change the file name "Jquery-1.7.2.js" to "jquery.js", you do not have to configure the paths parameter.
The supported AMD code in jquery is as follows
| 123 |
if( typeof define === "function" && define.amd && define.amd.jQuery ) { define( "jquery", [], function () { returnjQuery; } );} |
We know that jquery eventually exposes the global jquery and $. As follows
| 12 |
// Expose jQuery to the global objectwindow.jQuery = window.$ = jQuery; |
If you apply jquery to modular development, you can not use the global, that is, can not be exposed. You need to use the Require function when using jquery.
The first parameter of the Require function here is an array, where the module name (string type) is stored, and the module in the array corresponds to the parameter one by one of the callback function. The example here is only one module, "jquery".
Place the directory R1 on Apache or another Web server and access the index.html.
Network requests are as follows
We saw that Main.js and Jquery-1.7.2.js, besides Require.js, had also been asked to come down. And they were requested by Requirejs.
The version of jquery will pop up on the page
This is a very simple example of using Requirejs to dynamically load jquery. The following knowledge points are used
1. Data-main Properties
2. Require.config method
3. Require function
Requirejs Getting Started