自己封装了一些 asp 的类库 o(∩_∩)o 哈哈 比较喜欢 javascript
2011-07-05 22:04 dbfox 阅读(355) 评论(0) 编辑 收藏 举报String.asp
//str1 起始字符串 str2 结束字符串 bool1 bool2 是否包含起始 结束 字符串 String.prototype.cut=function(str1,str2,bool1,bool2){ var inx1=this.indexOf(str1); var inx2=this.indexOf(str2); var returnstr=""; if(bool1){ if(bool2){ returnstr=this.substr(inx1,inx2-inx1+1); }else{ returnstr=this.substr(inx1,inx2-inx1); } }else{ if(bool2){ returnstr=this.substr(inx1+1,inx2-inx1); }else{ returnstr=this.substr(inx1+1,inx2-inx1-1); } } return returnstr; } String.prototype.Trim=function(){ return this.replace(/(^\s*)|(\s*$)/g, ""); } String.prototype.htmlEntity=function(){ html=this .replace(/\&/g,"&") .replace(/</g,"<") .replace(/>/g,">") .replace(/"/g,""") .replace(/'/g,"'"); return html; } String.prototype.entityHtml=function(){ entity=this .replace(/\&/g,"&") .replace(/\</g,"<") .replace(/\>/g,">") .replace(/\"/g,'"') .replace(/\'/g,"'"); return entity; }
XmlHttp.asp
var XmlHttp={}; (function(obj){ var _xmlHttp; try{ _xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); }catch (e){ _xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); } obj.Get=function(_url,_fun){ _xmlHttp.onreadystatechange=function() { if(_xmlHttp.readyState==4&&_xmlHttp.status==200) { _fun(_xmlHttp.responseText); } } _xmlHttp.open("GET",_url,true); _xmlHttp.send(null); } })(XmlHttp);
IO.asp
var IO={}; (function(obj){ var FSO = new ActiveXObject("Scripting.FileSystemObject"); obj.File={ Read:function(path){ var ForReading = 1; var File=FSO.OpenTextFile(path,ForReading); return File.ReadAll(); }, Create:function(path,content){ var File = FSO.CreateTextFile(path, true); File.Write(content); File.Close(); }, Exist:function(path){ return FSO.FileExists(path); }, Delete:function(path){ if(this.Exist(path)){ FSO.DeleteFile(path); } } } IO.Folder={ Create:function(path){ FSO.CreateFolder(path); }, Delete:function(path){ if(this.Exist(path)){ FSO.DeleteFolder(path); } }, Exist:function(path){ return FSO.FolderExists(path); } } })(IO);
DB.asp
var DB; DB=function(){ var SetConfig = function (config, args) { if (args) { for (var proto in args) { config[proto] = args[proto]; } } return config; } var obj=this; obj.ConnectionString=""; obj.List={} obj.List.Page={ PS:25,//pagesize AP:1,//abspagecode PC:0,//pagecount RC:0 //recordCount } obj.Reader=function(){ var Conn=Server.CreateObject("ADODB.Connection"); var Rs=Server.CreateObject("ADODB.RecordSet"); this.Open=function(sql){ Conn.Open(obj.ConnectionString); Rs.Open(sql,Conn,1,1); } this.Read=function(){ if(Rs.EOF) { return false; }else{ for(var i=0;i<Rs.Fields.Count;i++){ this[Rs.Fields.Item(i).Name] = Rs.Fields.Item(i).Value; } Rs.MoveNext(); return true; } } this.Close=function() { Rs.Close(); Rs=null; Conn.Close(); Conn=null; } } obj.List.Config={}; //不够优化还要重构 obj.List.Get=function(){ var config = obj.List.Config =new SetConfig({ fields:" * ", table:null, where:" 1=1 ", sort:" ID desc ", pk:" ID " },arguments[0]); var RC=obj.GetRecord("select count("+config.pk+") as [count] from "+config.table+ " where "+config.where).count; var _MOD=RC%obj.List.Page.PS; if(_MOD==0){ obj.List.Page.PC=RC/obj.List.Page.PS; }else{ obj.List.Page.PC=(RC-_MOD)/obj.List.Page.PS+1; } obj.List.Page.RC=RC; } obj.List.GetDataTable=function(p){ var config = obj.List.Config; obj.List.Page.AP=p; var sql=null; if(p>1){ sql="select top "+obj.List.Page.PS+" "+config.fields+ " from "+config.table+" where "+config.where+ " and "+config.pk+ " not in(select top "+(p-1)*obj.List.Page.PS+" "+config.pk+ " from "+config.table+" where "+config.where+ " order by "+config.sort+") order by "+config.sort; }else{ sql="select top "+obj.List.Page.PS+" "+config.fields+ " from "+config.table+" where "+config.where+" order by "+config.sort; } var returnArr=[]; var Conn=Server.CreateObject("ADODB.Connection"); Conn.Open(obj.ConnectionString); var Rs=Server.CreateObject("ADODB.RecordSet"); Rs.Open(sql,Conn,1,1); while(!Rs.EOF){ var Record={}; for(var i=0;i<Rs.Fields.Count;i++){ Record[Rs.Fields.Item(i).Name] = Rs.Fields.Item(i).Value; } returnArr.push(Record); Rs.MoveNext(); } Rs.Close(); Rs=null; Conn.Close(); Conn=null; return returnArr; } obj.GetRecord = function(sql){ var Record=null; var Conn=Server.CreateObject("ADODB.Connection"); Conn.Open(obj.ConnectionString); var Rs=Server.CreateObject("ADODB.RecordSet"); Rs.Open(sql,Conn,1,1); if(Rs.RecordCount==1){ Record={}; for(var i=0;i<Rs.Fields.Count;i++){ Record[Rs.Fields.Item(i).Name] = Rs.Fields.Item(i).Value; } } Rs.Close(); Rs=null; Conn.Close(); Conn=null; return Record; } obj.ExecuteSQL=function(sql){ var Conn=Server.CreateObject("ADODB.RecordSet"); Conn.Open(obj.ConnectionString); Conn.Execute(sql); Conn.Close(); Conn=null; } obj.ADDRecord=function(sql,json){ var Conn=Server.CreateObject("ADODB.Connection"); Conn.Open(obj.ConnectionString); var Rs=Server.CreateObject("ADODB.RecordSet"); Rs.Open(sql,Conn,3,2); Rs.AddNew(); for(var inx in json){ Rs(inx).Value = json[inx]; } Rs.Update(); Rs.Close(); Rs=null; Conn.Close(); Conn=null; } obj.UpdateRecord = function(sql,json){ var Conn=Server.CreateObject("ADODB.Connection"); Conn.Open(obj.ConnectionString); var Rs=Server.CreateObject("ADODB.RecordSet"); Rs.Open(sql,Conn,3,2); for(var inx in json){ Rs(inx).Value = json[inx]; } Rs.Update(); Rs.Close(); Rs=null; Conn.Close(); Conn=null; } };
Page.asp
var Page; Page=function(p,pc,size,prefix,suffix){ p<1?p=1:p; p>pc?p=pc:p; size>pc?size=pc:size; this.getCode=function(cssNormal,cssSelect){ var code=""; if(pc<=size){ for(var i = 1;i<=pc; i++){ if(i==p){ code += ' <span'+(cssSelect?' class="'+cssSelect+'"':'')+'>'+i+'</span> '; }else{ code += ' <a'+(cssNormal?' class="'+cssNormal+'"':'')+' href="'+prefix+i+suffix+'">'+i+'</a> '; } } }else{ var minx = (size/2).toFixed(0); var sinx = p-minx+1; sinx=(sinx>1 ? sinx:1); //开始 einx=(pc+1>sinx+size?sinx+size:pc+1); //结束 for(var i=sinx;i<einx;i++){ if(i==p){ code += ' <span'+(cssSelect?' class="'+cssSelect+'"':'')+'>'+i+'</span> '; }else{ code += ' <a'+(cssNormal?' class="'+cssNormal+'"':'')+' href="'+prefix+i+suffix+'">'+i+'</a> '; } } } return code; } this.getPrev=function(css){ return p>1?'<a'+(css?' class="'+css+'"':'')+' href="'+prefix+(p-1)+suffix+'">上一页</a>':''; } this.getNext=function(css){ return p<pc?'<a'+(css?' class="'+css+'"':'')+' href="'+prefix+(p+1)+suffix+'">下一页</a>':''; } }
Format.asp
var Format={ Single:function(format,data){ var returnstr=""; for(var i=0;i<data.length;i++){ var temhtml=format.replace('[i]',i+1); for(var key in data[i]){ temhtml=temhtml.replace('{'+key+'}',data[i][key]); } returnstr+=temhtml; } return returnstr; }, Multi:function(format,data){ var returnstr=""; var inx=0; for(var i=0;i<format.length;i++){ if(inx>=data.length) break; if(!format[i].num) format[i].num=100; for(var j=0;j<format[i].num;j++){ var temhtml=format[i].html.replace('[i]',inx+1); for(var key in data[i]){ temhtml=temhtml.replace('{'+key+'}',data[i][key]); } returnstr+=temhtml; if(++inx>=data.length) break; } } return returnstr; } }; /* var jsonarr=[ {name:1,value:2}, {name:1,value:2}, {name:1,value:2}, {name:1,value:2}, {name:1,value:2}, {name:1,value:2}, {name:1,value:2} ] var arr=[ {num:1,html:'<li class="css1">{name} [i]:{value}</li>'}, {num:2,html:'<li class="css2">{name} [i]:{value}</li>'}, {html:'<li class="css3">{name} [i]:{value}</li>'} ] format.Multi(arr,jsonarr) */