_javascript technique of Automatic Update function

Source: Internet
Author: User
Tags base64 gettext
Copy Code code as follows:



function Class_autoupdate (Instancename,win)


{


This.instancename = instancename;


This.updating = false;


This.error = false;


This.updateinfo = "";


This.nextversion = "";


This.ver = "";


This.win = win;


This.updatepath = "";


This.updatefilename = "";


This.softname = "";


This.refresh ();





}


CLASS_AUTOUPDATE.prototype.onafterupdate = function ()


{





var _window = This.win;


var tempmessagewidth = 360;


var tempmessageheight = 160;


var tempmessageleft = (document.body.clientwidth-tempmessagewidth)/2;


var tempmessagetop = (document.body.clientheight-tempmessageheight)/2;


var feature = {


Title: "Update Completed",


Width:tempmessagewidth,


Height:tempmessageheight,


Left:tempmessageleft,


Top:tempmessagetop,


BorderColor: "#8B89A1",


BackgroundColor: "#FFFFFF",


Color: "#000000",


Titlebackgroundcolor: "#8B89A1",


Titlecolor: "#FFFFFF",


Dragable:true


};





var this = this;





var tempmessagecontent = [];





Tempmessagecontent[tempmessagecontent.length] = "<div style= ' font-size:12px;padding:5px ' >";


Tempmessagecontent[tempmessagecontent.length] = This.updateinfo;


Tempmessagecontent[tempmessagecontent.length] = "<br/><br/>";


Tempmessagecontent[tempmessagecontent.length] = "<div align=center><span class= ' bt ' ><input type= '" Button ' value= ' OK ' class= ' bt ' id= ' BT_UPDATE_OK ' ></span></div> ';


Tempmessagecontent[tempmessagecontent.length] = "</div>";





_window.open ("Updateok", feature);


_window.write ("Updateok", Tempmessagecontent.join (""), false);





_window.items["Updateok"].all ("Bt_update_ok"). onclick = function ()


{


_window.close ("Updateok");


}


_window.items["Updateok"].onunload = function ()


{


if (this.error==false&&this.updatefilelist.length>0)


{


Document.location.reload ();


}


}





}


CLASS_AUTOUPDATE.prototype.check = function ()


{


var _current_version,_new_version,_exist_new_ver;








Check configuration file


var configxml = new Class_xml ("Xml\\autoupdate.xml");


if (Configxml.error==false)


{


var _update_date,_autoupdate_day,_current_version;





var _save_or_not;





var _d = new Date ();


var _dd = _d.getfullyear () + "-" + (_d.getmonth () +1) + "-" + _d.getdate ();





Check for updated time period settings


if (Configxml.selectsinglenode ("/config/updatedate") ==null)


{


Configxml.settext ("/config", "Updatedate", _DD);





_update_date = _DD;


_save_or_not = true;


}


Else


{


_update_date = Configxml.gettext ("/config", "Updatedate", "1900-01-01");


}





if (Configxml.selectsinglenode ("/config/autoupdateday") ==null)


{


Configxml.settext ("/config", "Autoupdateday", "10");





_autoupdate_day = "10";


_save_or_not = true;


}


Else


{


_autoupdate_day = Configxml.gettext ("/config", "Autoupdateday", "10");


}


_autoupdate_day = _autoupdate_day *1;





if (Configxml.selectsinglenode ("/config/currentversion") ==null)


{


Configxml.settext ("/config", "CurrentVersion", "0.32");





_current_version = "0.32";


_save_or_not = true;


}


Else


{


_current_version = Configxml.gettext ("/config", "CurrentVersion", "0.32");


}


_current_version = _current_version * 1;








Determine if you want to connect to the server today


var _od = new Date (_update_date.replace (/-/g, "\/"));


if (_d.gettime ()-_od.gettime () >_autoupdate_day*24*3600*1000)


{





var newdoc = new Class_xml ();


Newdoc.loadremotefile (This.updatepath + this.softname + "/" + this.updatefilename);





if (Newdoc.selectsinglenode ("/config/currentversion") ==null)


{


Newdoc.settext ("/config", "CurrentVersion", "0.32");


_new_version = "0.32";


}


Else


{


_new_version = Newdoc.gettext ("/config", "CurrentVersion", "0.32");


}


_new_version = _new_version * 1;





Check to see if a new version is available


if (_new_version>_current_version)


{


_exist_new_ver = true;


}





Configxml.settext ("/config", "Updatedate", _DD);


_save_or_not = true;


}





if (_save_or_not)


{


Configxml.save ();


}





}





if (_exist_new_ver)


{


var _window = This.win;


var tempmessagewidth = 260;


var tempmessageheight = 120;


var tempmessageleft = (document.body.clientwidth-tempmessagewidth)/2;


var tempmessagetop = (document.body.clientheight-tempmessageheight)/2;


var feature = {


Title: "Upgrade",


Width:tempmessagewidth,


Height:tempmessageheight,


Left:tempmessageleft,


Top:tempmessagetop,


BorderColor: "#8B89A1",


BackgroundColor: "#FFFFFF",


Color: "#000000",


Titlebackgroundcolor: "#8B89A1",


Titlecolor: "#FFFFFF",


Dragable:true


};





var this = this;





var tempmessagecontent = [];





Tempmessagecontent[tempmessagecontent.length] = "<div style= ' font-size:12px;padding:5px ' >";


Tempmessagecontent[tempmessagecontent.length] = "<table width= ' 100% ' height= ' cellpadding= '" ><tr> <TD valign= ' top ' > has a new version, is it updated? <!--insert//--></td></tr></table> ";


Tempmessagecontent[tempmessagecontent.length] = "<br/><br/>";


Tempmessagecontent[tempmessagecontent.length] = "<div align=center><span class= ' bt ' ><input type= '" Button ' value= ' Yes ' class= ' bt ' id= ' Bt_update_yes ' ></span> <span class= ' bt ' ><input type= ' button ' Value= ' No ' class= ' bt ' id= ' bt_update_no ' ></span></div> ';


Tempmessagecontent[tempmessagecontent.length] = "</div>";





_window.open ("Update_or_not", feature);


_window.write ("Update_or_not", Tempmessagecontent.join (""), false);





This.ver = _current_version;





_window.items["Update_or_not"].all ("Bt_update_yes"). onclick = function ()


{


This.ver = _new_version;


_window.close ("Update_or_not");





This.update ();


}


_window.items["Update_or_not"].all ("Bt_update_no"). onclick = function ()


{


_window.close ("Update_or_not");


}


_window.items["Update_or_not"].onunload = function ()


{





}





}








}





CLASS_AUTOUPDATE.prototype.update = function ()


{





this.updating =!this.updating;





var t = new Date ();


var this = this;





if (This.error = = False)


{





Get a new configuration document


var newdoc = new Class_xml ();


Newdoc.loadremotefile (This.updatepath + this.softname + "/" + this.updatefilename);





This.error = Newdoc.error;





if (this.error)


{


This.updating = false;


This.updateinfo = "<table width= ' 100% ' height= ' ' cellpadding= ' ><tr><td '" the top ' > Connection Server failed & lt;! --insert//--></td></tr></table> ";


}


Else


{


var updatefilenodes = newdoc.selectnodes ("//systemfile");


var updateinfonode = Newdoc.selectsinglenode ("//updateinfo");


var nextversion = Newdoc.selectsinglenode ("//nextversion");





if (Updateinfonode!= null)


{


This.updateinfo = Updateinfonode.childnodes[0].text;


}





if (nextversion!= null)


{


This.nextversion = Nextversion.childnodes[0].text;


}





This.updatefilelist = [];





for (Var i=0;i<updatefilenodes.length;i++)


{


var curupdatefilename = updatefilenodes[i].getattribute ("name"). toLowerCase ();


var curupdatefileversion = Updatefilenodes[i].getattribute ("version"). toLowerCase ();


var Curupdatefilepath = updatefilenodes[i].getattribute ("path"). toLowerCase ();





if (this.systemfiles[curupdatefilename] = = NULL | | This.systemfiles[curupdatefilename]!= curupdatefileversion)


{


The file or version number is not consistent locally


This.updatefilelist[this.updatefilelist.length] =


{


Name:curupdatefilename,


Version:curupdatefileversion,


Path:curupdatefilepath,


Type: "Text"


};


}


}





Newdoc.dispose ();








if (THIS.updateFileList.length = 0)


{


This.updating = false;


This.updateinfo = "<table width= ' 100% ' height= ' ' cellpadding= ' ><tr><td '" the Top ' > No files need to be updated <!--insert//--></td></tr></table> ";


}


Else


{


var configxml = new Class_xml ("Xml\\autoupdate.xml");


Add update information


if (configxml.error==false&&this.updateinfo.length>0)


{


var _updateinfo = configxml.createelement ("Updateinfo");


var _cdata = configxml.createcdatasection (This.updateinfo);





_updateinfo. appendchild (_cdata);


Configxml.root (). appendchild (_updateinfo);





Add next version Trailer


var _nextversion = Configxml.selectsinglenode ("//nextversion");


if (_nextversion==null)


{


var __next = configxml.createelement ("nextversion");


var __cdata= configxml.createcdatasection (this.nextversion);





__next.appendchild (__cdata);





Configxml.root (). appendchild (__next);


}


Else


{


if (_nextversion.childnodes.length==0)


{


_nextversion.appendchild (Configxml.createcdatasection (""));


}


_nextversion.childnodes[0].text = this.nextversion;


}





Configxml.settext ("/config", "CurrentVersion", this.ver);





Configxml.save ();


}


Configxml.dispose ();





This.updatefile ();


}


}


}


Else


{


This.updateinfo = "<table width= ' 100% ' height= ' cellpadding= ' ><tr><td ' the ' top '" > Load config file failed! <!--insert//--></td></tr></table> ";


}


This.updating = false;


This.onafterupdate ();





}





CLASS_AUTOUPDATE.prototype.updateFile = function (Curupdateid)


{





var t = new Date ();


var this = this;





for (var i= 0;i<this.updatefilelist.length;i++)


{


var curupdatefile = this.updatefilelist[i];





if (Curupdatefile.type = = "Base64")


{


var url = this.updatepath + this.softname + "/base64.aspx?src=" + curupdatefile.name + "&t=" + t.valueof ();


}


else if (Curupdatefile.type = = "Text")


{


var url = this.updatepath + this.softname + "/" + Curupdatefile.path + "/" + curupdatefile.name;


}





Download files


var errors = This.downloadfile (Url,curupdatefile.path,curupdatefile.name);





if (This.error==false)


{


Log Current version information


var configxml = new Class_xml ("Xml\\autoupdate.xml");


if (Configxml.error==false)


{


var systemfiles = Configxml.selectsinglenode ("//systemfiles");


if (systemfiles = null)


{


Systemfiles = Configxml.createelement ("Systemfiles");


Configxml.selectsinglenode ("/config"). AppendChild (Systemfiles);


}





var systemfile = Configxml.selectsinglenode ("//systemfiles/systemfile[@name = '" + curUpdateFile.name.toLowerCase () + " ']");





if (systemfile = null)


{


Systemfile = Configxml.createelement ("Systemfile");


Systemfile.setattribute ("Name", CurUpdateFile.name.toLowerCase ());


Systemfile.setattribute ("Path", CurUpdateFile.path.toLowerCase ());


Configxml.selectsinglenode ("//systemfiles"). AppendChild (Systemfile);


}


Systemfile.setattribute ("version", curupdatefile.version);





Configxml.save ();


Configxml.dispose ();





This.error = false;


This.updateinfo + = errors + "\ n";


}


Else


{


This.updateinfo + = "<table width= ' 100% ' height= ' cellpadding= ' ><tr><td ' the valign= ' top ' > Error loading configuration file! <!--insert//--></td></tr></table> ";


}


}


}





}





CLASS_AUTOUPDATE.prototype.downloadFile = function (url,path,filename)


{


Try


{


var XmlHTTP = new ActiveXObject ("Microsoft.XMLHTTP");





Xmlhttp.open ("Get", url,false);


Xmlhttp.send ();





if (xmlhttp.readystate==4&&xmlhttp.status==200)


{


var adodbstream = new ActiveXObject ("ADODB" + "." + "Stream");





To determine if a directory exists, if it does not exist, create


This.buildpath (path);





var strlocalurl = Path.length>0?path + "\" +filename:filename;





1=adtypebinary


Adodbstream.type= 1;


Adodbstream.open ();


Adodbstream.write (Xmlhttp.responsebody);


Adodbstream.savetofile (strlocalurl,2);


Adodbstream.close ();





Adodbstream = null;


XmlHTTP = null;


}


Else


{


This.error = true;


return filename + "Download failed";


}





Return "";


}


catch (E)


{


This.error = true;


return e.description;


}


}


CLASS_AUTOUPDATE.prototype.refresh = function ()


{


var configxml = new Class_xml ("Xml\\autoupdate.xml");


if (Configxml.error==false)


{


var _softname = Configxml.selectsinglenode ("/config/softname");


var _updatepath = Configxml.selectsinglenode ("/config/updatepath");


var _updatefilename = Configxml.selectsinglenode ("/config/updatefilename");





This.updatepath = _updatepath!=null?_updatepath.text: "http://dooit.3322.org/";


This.softname = _softname!=null?_softname.text: "CC";


This.updatefilename = _updatefilename!=null?_updatefilename.text: "Autoupdate.xml";





This.systemfiles = {};


var tempsystemfilenodes = configxml.selectnodes ("/config/systemfiles/systemfile");


for (Var i=0;i<tempsystemfilenodes.length;i++)


{


This.systemfiles[tempsystemfilenodes[i].getattribute ("name")] = Tempsystemfilenodes[i].getattribute ("version");


}


}


Else


{


This.error = true;


}


Configxml.dispose ();


}


CLASS_AUTOUPDATE.prototype.buildPath = function (path)


{


var _baseurl = unescape (document.location.href.substring (Document.location.href.lastIndexOf ("/") +1,-1)). Replace (/ ^file\:\/\/\//i, ""). Replace (/\//g, "\ \");


var _path = Path.replace (/^\s*|\s*$/g, "");


var _fullpath = "";





Get full path


if (/^\w\:\\/.test (_path) = = False) {


_fullpath = _baseurl + _path;


}else{


_fullpath = _path;


}





var p = _fullpath.split (/\\|\//g);


var fso = new ActiveXObject ("Scripting.FileSystemObject");





var t = "";





for (Var i=0;i<p.length;i++) {


T + + p[i] + "\";





Try


{


if (!FSO. FolderExists (t))


{


Fso. CreateFolder (t);


}


}


catch (E)


{


return false;


}


}





return true;


}


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.