There are many mature js module loaders, such as requirejs and seajs, but for some small projects, their functions may be too "powerful". Maybe we just want one that can dynamically load js Function, maybe we just want to pretend that the page we write does not have a lot of things like . The two js loaders mentioned earlier emphasize modularity, that is, the organization and management of js files, and are more suitable for large projects.
As I just said, I just want to have a js file loader, and I only need to provide it with the address of a js file? Then enter our theme, use headjs. headjs is actually a complete set of tools, but what I just want to introduce is its Javascript Loader function. The following is its basic usage:
head.js("/path/to/file.js");
The simplest usage is to provide it with the address of a js file as parameters, and then it will load silently and non-blockingly behind the scenes. As for when it will be loaded and available, no one knows.
head.js("/path/to/file.js", function() { /*js加载完成*/ });
The most basic usage, in addition to providing an address parameter, also provides a callback function as the second parameter. After the js is loaded, the callback function is called. You can write the code that depends on the js in the callback function.
head.js("file1.js", "file2.js", ... "fileN.js");
Provide multiple js file addresses, these js will be processed in parallel Load, but these files will be executed in the order given by the parameters. For example, even if file2.js is loaded before file1.js, it will wait until file1.js is loaded and executed.
head.js("file1.js", "file2.js", function() { });
Load multiple js in parallel and execute them in parameter order. When all js are ready, the callback function is executed.
head.js("file1.js"); head.js("file2.js"); head.js("file3.js");
Multiple js are loaded in parallel, and whoever loads first will be executed first
head.js("file1.js") .js("file1.js").js("file3.js");
Chain calling method of the previous method
In this case, it is basically easy to use headjs to load js files It's enough, and it can also handle dependencies. If you continue to install B and build a website, hoping to use all the special effects, find a jquery plug-in here, find another one there, in short, you will create a lot of files with complex dependencies, what will happen? manage? Is there any need to ask? The above mentioned usages can completely solve it. But then I thought about it, since I installed B, let’s install it to the end. Now it’s not a popular module or something, so let’s install the entire module, but it’s not as complicated as commonJS said, that is, define a module, and then stipulate that this module has What are the js files composed of, what are the dependencies among them, etc. Then let’s do it. headjs uses the MTI protocol. It should be no problem to modify it. Diaosi is pretending to be tall, rich and handsome again. In fact, it is a modification, but it is better to say that it is to add a few pieces of code, and functions such as loading dependencies are completely implemented using the headjs API.
Here, I added two new methods to headjs. One is add(name, file, preload) to add a module. The parameter name is the module name, and file is the js file address to be used. If there is If there are multiple files and there are dependencies, file can be an array. The elements in this array are the file addresses used, and dependencies are performed in the order of the array elements. The third parameter preload is used to specify whether to preload the module file. If it is false, the file will be loaded when the module is used. If it is true, the file used will be loaded immediately when the module is defined. The default is false
The other is use(name,callback), this method is used to use the module. name is the module name defined using the add() method, and callback is the callback function called after the module has been loaded.
So we can use headjs like this:
head.add('jqueryui',[jquery.js,jqueryui.js]);//定义模块 head.use('jqueryui',function(){ //可以使用jqueryui啦 });
If there are many modules, the module definition code can be written in a separate file , and after introducing headjs, the module definition file is loaded immediately. This process can be easily implemented like this:
See the init attribute No, I call it the initialization attribute. The value of init represents the file name. For example, init above represents the file init.js in the same directory as headjs. The so-called initialization means that the init file must have been loaded before using use(). So you can write the module definition in the initialization file.
Nima, what kind of trouble is this? There is another main attribute, okay, I admit that I saw someone else having this thing, so I followed suit and got one too. The main attribute represents the module name, that is, after the head.js and init.js files are loaded, the module specified by the main attribute will be automatically executed (of course, the module must be defined in init first). So with these two things, we can execute very complex code without a single js code on the page (except for the tag that introduces headjs).
There is another point that needs to be explained, which is the storage location of the module files. The module file should be stored in the same directory as head.js, such as head.add('a','a.js'). The path of a.js here is the same as head.js, which is the path of the module file. It is relative to the path where head.js is stored, and can only go down, not up, for example:
head.add('a','a/a.js')//Correct
head.add('a','../a.js')//Error, cannot be positioned upward
Finally, put the modified head.js source file, It's not a compressed version, there are comments on the changes.
The above is the entire content of this article. I hope it will be helpful to everyone. I also hope to support the PHP Chinese website!
For more related articles about using headjs to manage and load js and improve website loading speed, please pay attention to the PHP Chinese website!

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.

I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing

This article demonstrates frontend integration with a backend secured by Permit, building a functional EdTech SaaS application using Next.js. The frontend fetches user permissions to control UI visibility and ensures API requests adhere to role-base

JavaScript is the core language of modern web development and is widely used for its diversity and flexibility. 1) Front-end development: build dynamic web pages and single-page applications through DOM operations and modern frameworks (such as React, Vue.js, Angular). 2) Server-side development: Node.js uses a non-blocking I/O model to handle high concurrency and real-time applications. 3) Mobile and desktop application development: cross-platform development is realized through ReactNative and Electron to improve development efficiency.

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

Python is more suitable for data science and machine learning, while JavaScript is more suitable for front-end and full-stack development. 1. Python is known for its concise syntax and rich library ecosystem, and is suitable for data analysis and web development. 2. JavaScript is the core of front-end development. Node.js supports server-side programming and is suitable for full-stack development.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 Linux new version
SublimeText3 Linux latest version

SublimeText3 Chinese version
Chinese version, very easy to use