您好,欢迎访问一九零五行业门户网

自动更新作用_javascript技巧

复制代码 代码如下:
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:更新完毕,
        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] = ;
    tempmessagecontent[tempmessagecontent.length] = this.updateinfo;
    //tempmessagecontent[tempmessagecontent.length] =
;
    tempmessagecontent[tempmessagecontent.length] = 
;
    tempmessagecontent[tempmessagecontent.length] = 
;
_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;
//检查配置文件
    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();
//检查是否有更新时间段设置
        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;
//判断是否今天去连接服务器
        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;
//检查是否有新版本
            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:升级,
            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] = ;
        tempmessagecontent[tempmessagecontent.length] = 有新版本,是否更新?
;
        //tempmessagecontent[tempmessagecontent.length] =
;
        tempmessagecontent[tempmessagecontent.length] =   
;
        tempmessagecontent[tempmessagecontent.length] = 
;
_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)
    {
//得到新配置文档
    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    = 连接服务器失败
;
        }
        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            {
                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)
                {                                    
                    //本地没有该文件或者版本号不一致
                    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    = 没有需要更新的文件
;
            }
            else
            {
                var configxml    = new class_xml(xml\\autoupdate.xml);
                //添加更新信息
                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);
//添加下版本预告
                    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 = 加载配置文件失败!
;
    }
    this.updating = false;
    this.onafterupdate();
}
class_autoupdate.prototype.updatefile = function(curupdateid)
{
var t        = new date();
    var this    = this;
for(var i= 0;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;
        }
//下载文件
        var errors     = this.downloadfile(url,curupdatefile.path,curupdatefile.name);
if(this.error==false)
        {
            //记录当前版本信息                    
            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 += 加载配置文件出错!
;
            }
        }
    }
}
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);
//判断目录是否存在,不存在则创建
            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 + 下载失败;
        }
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        {
            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    = ;
//得到全路径
    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        t += p[i] + \\;
try
        {
            if(!fso.folderexists(t))
            {
                fso.createfolder(t);
            }
        }
        catch(e)
        {
            return false;
        }
    }
return true;
}
其它类似信息

推荐信息