Code _javascript techniques for dynamically loading/deleting/updating external javascript/css files

Source: Internet
Author: User
Dynamically Loading JAVASCRIPT/CSSS files
The traditional way to load external JavaScript (*.js) or Css (*.css) files is to add them directly to the
Copy Code code as follows:

<script type= "Text/javascript" src= "Myscript.js" ></script>
<link rel= "stylesheet" type= "Text/css" href= "Main.css"/>


These files are loaded synchronously into the current page in this way.

Now load the Javascript/css file in a dynamic way:

Create a "script" or "link" element with the DOM createelement method
Set the appropriate properties
Use the AppendChild method to insert the created element at the end of the head label
Copy Code code as follows:

function loadjscssfile (filename, filetype) {
If the file type is. js, the script label is created and the corresponding property is set
if (filetype== "JS") {
var fileref=document.createelement (' script ');
Fileref.setattribute ("type", "Text/javascript");
Fileref.setattribute ("src", filename);
}
If the file type is. CSS, the script label is created and the corresponding property is set
else if (filetype== "CSS") {
var fileref=document.createelement ("link");
Fileref.setattribute ("rel", "stylesheet");
Fileref.setattribute ("type", "text/css");
Fileref.setattribute ("href", filename);
}
if (typeof fileref!= "undefined")
document.getElementsByTagName ("Head") [0].appendchild (FILEREF);
}
Dynamically add a. js file
Loadjscssfile ("Myscript.js", "JS");
Add a. php file dynamically, just as you would add a. js file
Loadjscssfile ("javascript.php", "JS");
Dynamic one. css file
Loadjscssfile ("Mystyle.css", "CSS");


To prevent multiple loading of the same Js/css file, add the following judgment (this is just a rough test)
Copy Code code as follows:

File name that is temporarily loaded
var filesadded= "";

function checkloadjscssfile (filename, filetype) {
if (Filesadded.indexof ("[" +filename+] ") ==-1) {
Loadjscssfile (filename, filetype);
deposit [filename] into filesadded
filesadded+= "[" +filename+ "]";
}
else{
Alert ("File already added!");
}

First load
Checkloadjscssfile ("Myscript.js", "JS");
Loading the same file repeatedly failed
Checkloadjscssfile ("Myscript.js", "JS");


Delete javascript/csss files dynamically
Note: There are bugs when you delete styles dynamically under IE6/7. 2 Solutions: 1. The style table does not have the import style sheet 2. Set the link's Type property to a null value, and then modify the href's place, or set the href to null, and finally set the type value to "text/css" to force IE to interpret the new stylesheet.

Get the appropriate DOM element
Locating elements by file name & file type
Delete a "script" or "link" element with DOM RemoveChild
Copy Code code as follows:

function removejscssfile (filename, filetype) {
Determining File Types
var targetelement= (filetype== "JS")? "Script": (filetype== "CSS")? "Link": "None";
Determine file name
var targetattr= (filetype== "JS")? "src": (filetype== "CSS")? "href": "None";
var allsuspects=document.getelementsbytagname (targetelement);
Iterate through the elements and delete the matching elements
for (var i=allsuspects.length; i>=0; i--) {
if (Allsuspects[i] && allsuspects[i].getattribute (targetattr)!=null && Allsuspects[i].getattribute ( targetattr). indexOf (filename)!=-1)
Allsuspects[i].parentnode.removechild (Allsuspects[i]);
}
}

Removejscssfile ("Somescript.js", "JS");
Removejscssfile ("Somestyle.css", "CSS");


Dynamically Update JAVASCRIPT/CSSS files
Use createelement to create the JAVASCRIPT/CSS element to update
Find the element to be replaced
Replacing elements with ReplaceChild
Copy Code code as follows:

function createjscssfile (filename, filetype) {
if (filetype== "JS") {//if filename is a external JavaScript file
var fileref=document.createelement (' script ')
Fileref.setattribute ("type", "Text/javascript")
Fileref.setattribute ("src", filename)
}
else if (filetype== "CSS") {//if filename is a external CSS file
var fileref=document.createelement ("link")
Fileref.setattribute ("rel", "stylesheet")
Fileref.setattribute ("type", "Text/css")
Fileref.setattribute ("href", filename)
}
return FileRef
}

function Replacejscssfile (oldfilename, newfilename, filetype) {
var targetelement= (filetype== "JS")? "Script": (filetype== "CSS")? "Link": "None";
var targetattr= (filetype== "JS")? "src": (filetype== "CSS")? "href": "None";
var allsuspects=document.getelementsbytagname (targetelement);
for (var i=allsuspects.length; i>=0; i--) {
if (Allsuspects[i] && allsuspects[i].getattribute (targetattr)!=null && Allsuspects[i].getattribute ( targetattr). IndexOf (oldfilename)!=-1) {
var newelement=createjscssfile (newfilename, filetype);
Allsuspects[i].parentnode.replacechild (Newelement, allsuspects[i]);
}
}
}
Replace "Oldscript.js" with "Newscript.js"
Replacejscssfile ("Oldscript.js", "Newscript.js", "JS");
Replace "Oldscript.css" with "Newscript.css"
Replacejscssfile ("Oldstyle.css", "Newscript.css", "CSS");

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.