search
HomeWeb Front-endHTML TutorialrequireJS loads different versions of jquery_html/css_WEB-ITnose in the same HTML/JSP page

requireJS supports JS modularization and can also load different versions of modules on the same HTML/JSP page. This feature is great, let’s see how to load multiple versions of jquery. Assume that in the HTML page, the data-main attribute entry file is main.js, and the directory structure where the file is stored is as follows:

test.html

require.js

main. js

scripts/

demo1.js

demo2.js

libs/

jquery-1.10.2.js

jquery-2.1.1.js

We write the following code in main.js:

//创建1个contextvar reqOne = requirejs.config({	baseUrl: 'scripts/libs/',	context:"context1",	paths:{		jquery: 'jquery-1.10.2'	}});		reqOne(['jquery'],		function($) {			console.log("v1=" + $().jquery);											});//创建1个context		var reqTwo  = requirejs.config({	baseUrl: 'scripts/libs/',	context:"context2",	paths:{		jquery: 'jquery-2.1.1'	}});	reqTwo(['require','jquery'],		function(require,$) {					console.log("v1=" + $().jquery);												});

Through this multi-context approach, we can Implement multi-version module coexistence. But it also leads to two problems:

1. What happens if the modules loaded in different contexts are the same and have the same version (the same js file)? Will this js file be downloaded multiple times? ?

Conclusion: Through httpwatch observation, if different contexts load the same js file, then this js file will be downloaded multiple times. Obviously this is wasteful.

2. If both contexts require jquery-1.10.1 version, then how to ensure that only js is downloaded once?

Conclusion: We can define a context similar to the parent class, Store public modules, and each context can access the rootContext.

//定义rootContextvar rootContext = require.config({ baseUrl: 'scripts/'});var reqOne = requirejs.config({ baseUrl: 'scripts/libs/', context:"context1", paths:{  jquery: 'jquery-1.10.2' }});reqOne(['jquery'],  function($) {   console.log("v1=" + $().jquery);      // 访问rootContext中的JS模块   rootContext(['demo1'],function(m){    console.log("require1=" + m.name);   });             });  var reqTwo  = requirejs.config({ baseUrl: 'scripts/libs/', context:"context2", paths:{  jquery: 'jquery-2.1.1' }});  reqTwo(['require','jquery'],  function(require,$) {      console.log("v1=" + $().jquery);      // 访问rootContext中的JS模块   rootContext(['demo1'],function(m){    console.log("require1=" + m.name);   });               });
Through httpwatch observation, you can see that demo1.js will only be downloaded once, which solves the problem of multiple downloads of the same file.


Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
What is the significance of <head> and <body> tag in HTML?What is the significance of <head> and <body> tag in HTML?Apr 28, 2025 pm 05:43 PM

The article discusses the roles of <head> and <body> tags in HTML, their impact on user experience, and SEO implications. Proper structuring enhances website functionality and search engine optimization.

Please explain how to indicate the character set being used by a document in HTML?Please explain how to indicate the character set being used by a document in HTML?Apr 28, 2025 pm 05:41 PM

Article discusses specifying character encoding in HTML, focusing on UTF-8. Main issue: ensuring correct display of text, preventing garbled characters, and enhancing SEO and accessibility.

What are the various formatting tags in HTML?What are the various formatting tags in HTML?Apr 28, 2025 pm 05:39 PM

The article discusses various HTML formatting tags used for structuring and styling web content, emphasizing their effects on text appearance and the importance of semantic tags for accessibility and SEO.

What is the difference between the 'id' attribute and the 'class' attribute of HTML elements?What is the difference between the 'id' attribute and the 'class' attribute of HTML elements?Apr 28, 2025 pm 05:39 PM

The article discusses the differences between HTML's 'id' and 'class' attributes, focusing on their uniqueness, purpose, CSS syntax, and specificity. It explains how their use impacts webpage styling and functionality, and provides best practices for

What is the 'class' attribute in HTML?What is the 'class' attribute in HTML?Apr 28, 2025 pm 05:37 PM

The article explains the HTML 'class' attribute's role in grouping elements for styling and JavaScript manipulation, contrasting it with the unique 'id' attribute.

What are different types of lists in HTML?What are different types of lists in HTML?Apr 28, 2025 pm 05:36 PM

Article discusses HTML list types: ordered (<ol>), unordered (<ul>), and description (<dl>). Focuses on creating and styling lists to enhance website design.

What are HTML Entities?What are HTML Entities?Apr 28, 2025 pm 05:35 PM

HTML entities are codes used in HTML to display special characters correctly, ensuring proper rendering across browsers and devices.

What is the advantage of collapsing white space?What is the advantage of collapsing white space?Apr 28, 2025 pm 05:35 PM

Collapsing white space optimizes content by reducing file size, enhancing readability, and ensuring consistent formatting across platforms in web and document processing.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool