Javascript load page,load css,load JS implementation code _JAVASCRIPT skills

Source: Internet
Author: User
Copy Code code as follows:

/***********************************************
* Ajax Page fetcher-by JavaScript Kit (www.javascriptkit.com)
***********************************************/
var ajaxpagefetcher={
Loadingmessage: "Loading Page, please wait ...",
exfilesadded: "",

Connect:function (Containerid, Pageurl, Bustcache, Jsfiles, Cssfiles) {
var page_request = False
var bustcacheparameter= ""
if (window. XMLHttpRequest)//If Mozilla, IE7, Safari etc
Page_request = new XMLHttpRequest ()
else if (window. ActiveXObject) {//if IE6 or below
try {
Page_request = new ActiveXObject ("Msxml2.xmlhttp")
}
catch (e) {
try{
Page_request = new ActiveXObject ("Microsoft.XMLHTTP")
}
catch (e) {}
}
}
Else
return False
var ajaxfriendlyurl=pageurl.replace (/^http:\/\/[^\/]+\//i, "http://" +window.location.hostname+ "/")
Page_request.onreadystatechange=function () {ajaxpagefetcher.loadpage (page_request, Containerid, Pageurl, Jsfiles, Cssfiles)}
if (Bustcache)//if bust caching of external page
Bustcacheparameter= (Ajaxfriendlyurl.indexof ("?")! =-1)? "&" +new Date (). GetTime (): "?" +new Date (). GetTime ()
document.getElementById (Containerid). Innerhtml=ajaxpagefetcher.loadingmessage//display "fetching page message"
Page_request.open (' Get ', Ajaxfriendlyurl+bustcacheparameter, True)
Page_request.send (NULL)
},

Loadpage:function (Page_request, Containerid, Pageurl, Jsfiles, Cssfiles) {
if (page_request.readystate = 4 && (page_request.status==200 | | window.location.href.indexOf ("http") ==-1)) {
document.getElementById (Containerid). Innerhtml=page_request.responsetext
for (var i=0; i<jsfiles.length; i++)
This.loadjscssfile (Jsfiles[i], "JS")
for (var i=0; i<cssfiles.length; i++)
This.loadjscssfile (Cssfiles[i], "CSS")
This.pageloadaction (pageurl)//invoke Custom "Onpageload" event
}
},

Createjscssfile:function (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
},

Loadjscssfile:function (filename, filetype) {//load or replace (if already exists) external. js and. css files
if (This.exfilesadded.indexOf ("[+filename+]") ==-1) {//if desired file to load hasnt already been
var newelement=this.createjscssfile (filename, filetype)
document.getElementsByTagName ("Head") [0].appendchild (Newelement)
this.exfilesadded+= "[" +filename+ "]"//remember this file as being added
}
else{//if file has been loaded already (Replace/refresh it)
var targetelement= (filetype== "JS")? "Script": (filetype== "CSS")? ' Link ': ' None '//determine element type to create nodelist using
var targetattr= (filetype== "JS")? "src": (filetype== "CSS")? "href": "None"//determine corresponding attribute to test for
var allsuspects=document.getelementsbytagname (targetelement)
For (Var i=allsuspects.length. i>=0; i--) {//search backwards within nodelist for matching elements to remove
if (Allsuspects[i] && allsuspects[i].getattribute (targetattr)!=null && Allsuspects[i].getattribute ( targetattr). indexOf (filename)!=-1) {
var newelement=this.createjscssfile (filename, filetype)
Allsuspects[i].parentnode.replacechild (Newelement, allsuspects[i])
}
}
}
},


Pageloadaction:function (Pageurl) {
This.onpageload (pageurl)//call Customize Onpageload () function when a Ajax page is fetched/loaded
},

Onpageload:function (Pageurl) {
Do No by default
},

Load:function (Containerid, Pageurl, Bustcache, Jsfiles, Cssfiles) {
var jsfiles= (typeof jsfiles== "undefined" | | jsfiles== "")? []: Jsfiles
var cssfiles= (typeof cssfiles== "undefined" | | cssfiles== "")? []: Cssfiles
This.connect (Containerid, Pageurl, Bustcache, Jsfiles, Cssfiles)
}

}//end Object

Sample usage:
1) ajaxpagefetcher.load ("Mydiv", "content.htm", True)
2) ajaxpagefetcher.load ("Mydiv2", "content.htm", True, ["External.js"])
3) ajaxpagefetcher.load ("Mydiv2", "content.htm", True, ["External.js"], ["EXTERNAL.CSS"])
4) ajaxpagefetcher.load ("Mydiv2", "content.htm", True, ["External.js", "External2.js"])
5) Ajaxpagefetcher.load ("Mydiv2", "content.htm", True, "", ["External.css", "External2.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.