Home > Web Front-end > JS Tutorial > Simple example of dynamically loading js files_javascript skills

Simple example of dynamically loading js files_javascript skills

WBOY
Release: 2016-05-16 15:04:32
Original
1317 people have browsed it

The example in this article describes the method of dynamically loading js files. Share it with everyone for your reference, the details are as follows:

function loadScript(url){
var hd = document.getElementsByTagName('head')[0],
js = document.createElement('script');
js.src = url;
js.type = "text/JavaScript";
if(js.addEventListener){
js.addEventListener("load", function(){alert('loaded');}, false);
}else if(js.attachEvent){
js.attachEvent("onreadystatechange", function(){
if(js.readyState === "loaded" || js.readyState === "complete"){
alert('loaded');
}
});
}
hd.appendChild(js);
}

Copy after login

Readers who are interested in more JavaScript-related content can check out the special topics on this site: "Summary of JavaScript switching effects and techniques", "Summary of JavaScript search algorithm techniques", "Summary of JavaScript animation special effects and techniques", "Summary of JavaScript errors and debugging skills", "Summary of JavaScript data structures and algorithm techniques", "JavaScript traversal Summary of Algorithms and Techniques" and "Summary of JavaScript Mathematical Operation Usage"

I hope this article will be helpful to everyone in JavaScript programming.

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template