CopyCode The Code is 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 This. loadjscssfile (jsfiles [I], "JS")
for (VAR I = 0; I This. loadjscssfile (cssfiles [I], "CSS")
This. pageloadaction (pageurl) // invoke custom "onpageload" event
}< BR >},
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 an 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 loaded
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
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 an Ajax page is fetched/loaded
},
Onpageload: function (pageurl ){
// Do nothing 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"])