Home Web Front-end JS Tutorial JavaScript development essay 2: Dynamically loading js and files_javascript skills

JavaScript development essay 2: Dynamically loading js and files_javascript skills

May 16, 2016 pm 05:59 PM
dynamic loading

The best way at this time is to introduce it on demand and dynamically introduce component js and styles. After the file is loaded, call the callback and run the js. The code is still very simple
1. Determine whether the file load is complete. The loading status ie is onreadystatechange, and the others are onload, onerror

Copy code The code is as follows:

if (isie){
Res.onreadystatechange = function(){
if(Res.readyState == 'complete' || Res.readyState == 'loaded'){
Res.onreadystatechange = null;
callback();
_self.loadedUi[modelName] = true;
}
}
}else{
Res.onload = function(){
Res.onload = null;
callback();
_self.loadedUi[modelName] = true;
}
Res.onerror = function(){
throw new Error('res error:' modelName ' .js');
}
}

2. It is best to keep the naming of all components consistent, and callback calls are also more convenient. You can also add parameters as needed, such as: requires, which depends on those files; style, true || false, whether to load the style, etc.
3. You can also remove scripts, style tags, and delete components
Copy code The code is as follows:

(function(window,undefined){
if(!window.ui) {
window.ui = {};
}
//Dynamic loading ui js
window.bus = {
config : {
version : window.config.version,
cssPath : window.config.resServer '/css/v3/ui',
jsPath : window.config.resServer '/js/v2/ui'
},
loadedUi : {},
readyStateChange : function(){
var ua = navigator.userAgent.toLowerCase();
return ua.indexOf('msie') >= 0;
},
loadRes : function(modelName,prames,callback){
var _self = this;
var Res = document.createElement(prames.tagName);
for(var k in prames){
if(k != 'tagName'){
Res.setAttribute(k,prames[k],0);
}
}
document.getElementsByTagName('head')[0].appendChild(Res);
if(this.readyStateChange()){
Res.onreadystatechange = function() {
if(Res.readyState == 'complete' || Res.readyState == 'loaded'){
Res.onreadystatechange = null;
callback();
_self.loadedUi[modelName ] = true;
}
}
}else{
Res.onload = function(){
Res.onload = null;
callback();
_self. loadedUi[modelName] = true;
}
Res.onerror = function(){
throw new Error('res error:' modelName '.js');
}
}
},
removeUi : function(modelName){
if(!modelName){
return true
};
this.loadedUi[modelName] = false;
var head = document.getElementsByTagName('head')[0];
var model_js = document.getElementById('J_model_' modelName '_js');
var model_css = document.getElementById('J_model_' modelName '_css') ;
if(model_js && model_css){
delete window.ui[modelName];
head.removeChild(model_js);
head.removeChild(model_css);
return true;
}else{
return false;
}
},
loadUi : function(modelName,callback,setting){
if(!modelName){
return true
};
callback = callback || function(){};
if(this.loadedUi[modelName] == true){
callback();
return true
}
var deafult_setting = {
style : true,
js : true,
requires : []
}
for(var key in setting){
deafult_setting[key] = setting [key];
}
deafult_setting['style'] === true && this.loadRes(modelName,{
id : 'J_model_' modelName '_css',
name : modelName,
tagName : 'link',
type : 'text/css',
rel : 'stylesheet',
href : this.config.cssPath '/' modelName '.css?v=' this .config.version
});
deafult_setting['js'] === true && this.loadRes(modelName,{
id : 'J_model_' modelName '_js',
name : modelName ,
tagName : 'script',
type : 'text/javascript',
src : this.config.jsPath '/' modelName '.js?v=' this.config.version
},callback);
if(deafult_setting.requires.length > 0){
for(var i=0,len = deafult_setting.requires.length;ithis. loadUi(deafult_setting.requires[i]);
}
}
}
}
})(window)

Usage
Copy code The code is as follows:

// load comment for feed
window.bus.loadUi('new_comment_feed', function(){
window.ui.new_comment_feed($("#J_newsList"));
},{
style : false,
requires:['emoticon','addFriend']
});
// load new yy ui
window.bus.loadUi('yy', function(){
window.ui.yy(options);
},{
style:false,
requires:['emoticon']
});
// load photoLightbox
window.bus.loadUi('photoLightbox', function(){
window.ui.photoLightbox(options.urlAjaxGetFriendPhoto, options.urlCommentOtherPhoto,$("#J_newsList"),options.myUid,options.myName);
});
Statement of this Website
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

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Python implements dynamic page loading and asynchronous request processing function analysis for headless browser collection applications Python implements dynamic page loading and asynchronous request processing function analysis for headless browser collection applications Aug 08, 2023 am 10:16 AM

Python implements the dynamic loading and asynchronous request processing functions of headless browser collection applications. In web crawlers, sometimes it is necessary to collect page content that uses dynamic loading or asynchronous requests. Traditional crawler tools have certain limitations in processing such pages, and cannot accurately obtain the content generated by JavaScript on the page. Using a headless browser can solve this problem. This article will introduce how to use Python to implement a headless browser to collect page content using dynamic loading and asynchronous requests.

How to handle dynamic loading and switching of components in Vue How to handle dynamic loading and switching of components in Vue Oct 15, 2023 pm 04:34 PM

Handling dynamic loading and switching of components in Vue Vue is a popular JavaScript framework that provides a variety of flexible functions to handle the dynamic loading and switching of components. In this article, we will discuss some methods of handling dynamic loading and switching of components in Vue, and provide specific code examples. Dynamically loading components means dynamically loading components at runtime as needed. This improves the performance and loading speed of your application because relevant components are loaded only when needed. Vue provides async and awa

Revealing the principle of hot update in Golang: insider explanation of dynamic loading and reloading Revealing the principle of hot update in Golang: insider explanation of dynamic loading and reloading Jan 20, 2024 am 10:09 AM

Exploring the Principle of Golang Hot Update: The Mystery of Dynamic Loading and Reloading Introduction: In the field of software development, programmers often hope to be able to modify and update code without restarting the application. Such requirements are of great significance to both development efficiency and system operation reliability. As a modern programming language, Golang provides developers with many convenient mechanisms to implement hot updates. This article will delve into the principles of Golang hot update, especially the mysteries of dynamic loading and reloading, and will combine it with specific code examples.

How to create a table that dynamically loads data using Vue and Element-UI How to create a table that dynamically loads data using Vue and Element-UI Jul 21, 2023 pm 11:49 PM

How to use Vue and Element-UI to create a table that dynamically loads data. In modern web development, data tables are one of the common interface components. Vue.js is a very popular front-end framework nowadays, and Element-UI is a set of component libraries developed based on Vue.js, which provides a rich set of UI components for us to use. This article will introduce how to use Vue and Element-UI to create a table that can dynamically load data, and give corresponding code examples. First, we need to install

Solve Vue error: Unable to correctly use Vue Router to dynamically load components based on routing parameters Solve Vue error: Unable to correctly use Vue Router to dynamically load components based on routing parameters Aug 20, 2023 am 08:09 AM

Solve Vue error: Unable to correctly use VueRouter to dynamically load components based on routing parameters. In the process of using VueRouter for routing jumps, sometimes we need to dynamically load components based on routing parameters. However, in some cases, we may encounter a common error: unable to correctly use VueRouter to dynamically load components based on routing parameters. This article will describe how to resolve this error and provide code examples. First, we need to make it clear: VueRouter can pass

How to use reflection and dynamically load assemblies in C# How to use reflection and dynamically load assemblies in C# Oct 08, 2023 pm 12:12 PM

How to use reflection and dynamically load assemblies in C# Introduction: In C#, reflection (Reflection) is a powerful mechanism that allows us to obtain and operate the metadata of the program at runtime, including type information, member information, etc. Dynamically loading assemblies is a common application implemented through reflection, and is very useful in some specific scenarios. This article will introduce in detail how to use reflection and dynamically load assemblies in C#, and provide specific code examples. The basic concept of reflection Reflection is an important function in the C# language

phpSpider practical tips: How to deal with dynamic loading of web content? phpSpider practical tips: How to deal with dynamic loading of web content? Jul 22, 2023 am 08:33 AM

phpSpider practical tips: How to deal with dynamic loading of web content? When crawling web page data, we often encounter the problem that dynamically loaded content cannot be obtained directly through the crawler. These dynamically loaded contents can be data obtained through AJAX requests, DOM elements rendered through JavaScript, etc. In order to solve this problem, this article will introduce some practical tips for dealing with dynamic loading problems of web pages when using phpSpider. 1. Use network debugging tools to find dynamically loaded URLs

Use the load() method of the System class in Java to dynamically load classes or resources Use the load() method of the System class in Java to dynamically load classes or resources Jul 25, 2023 am 10:25 AM

Use the load() method of the System class in Java to dynamically load classes or resources. In Java development, sometimes we need to dynamically load classes or resources while the program is running to achieve some flexible functions. Java provides the load() method of the System class to achieve this requirement. This article will introduce the use of the load() method of the System class and provide corresponding code examples. First, let’s understand the definition of the load() method: publicstaticvo

See all articles