


requireJS 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.

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.

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.

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.

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

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

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

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

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


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

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

Hot Article

Hot Tools

WebStorm Mac version
Useful JavaScript development tools

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
Powerful PHP integrated development environment

SublimeText3 Chinese version
Chinese version, very easy to use

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool
