Home  >  Article  >  Web Front-end  >  Summary of various solutions to dynamically loading js files in javascript_javascript skills

Summary of various solutions to dynamically loading js files in javascript_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:14:451108browse

A comparison method is all added dynamically

Copy code The code is as follows:

/*
Dynamically load js v1.0 by: dum
Usage: src="webJsBase.js?load=a,b"
Note: Load js in this directory
*/
var webJsBase = {
require: function(libraryName) {
document.write('');
},
load: function(defaultLoad) {
if((typeof Prototype=='undefined')||(typeof Element == 'undefined')||(typeof Element.Methods=='undefined') )
throw ('prototype lib loading failed!');
if(typeof defaultLoad=='undefined')defaultLoad='';
var js = /webJsBase.js(?.*)?$ //;
var path = s.src.replace(js, '');
var includes = s.src.match(/?.*load=([a-zA-Z0-9_,]*)/ ; ;
});
}
};
webJsBase.load(); //This parameter can specify the js file to be loaded by default



This is the simplest method to use document.write directly after loading, as shown below.


Copy code


Copy the code


The code is as follows:

Here we use getElementsByTagName('HEAD') to dynamically create script elements


Copy code

Copy the code



Call the method, so it looks like your php include function



Copy code

Code As follows:include(baseDir "/Prototype.js");include(baseDir "/Map.js");include(baseDir "/MapEvent.js");
include(baseDir "/model/MapModel.js");
include(baseDir "/model/MapType.js");
include(baseDir "/model/Tile.js");

어떤 친구들은 ExtJs4를 사용하여 js를 동적으로 로드할 수 있다고 말했습니다. 여기서는 위의 방법만으로 js를 동적으로 로드할 수 있습니다.

따라서 이 방법을 사용하여 Js를 동적으로 로드하는 동안 기본 인터페이스의 Js 스크립트가 계속 실행되므로 비동기적으로 로드된 Js 코드는 예상한 효과를 얻지 못할 수 있습니다.

이때 Ajax를 사용하여 Js를 로드하는 것을 고려할 수 있습니다.

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