首页 > 网站 > WEB开发 > 正文

我写的javascript常用静态方法类,分享给大家

2024-04-27 14:14:45
字体:
来源:转载
供稿:网友

我写的javascript常用静态方法类,分享给大家

util=function(){return{$:function(id){returndocument.getElementById(id);},trim:function(str){returnstr.replace(/(^/s+)|(/s+$)/g,"");},len:function(str){returnstr.replace(/[^/x00-/xff]/g,'**').length;},format:function(str){vararg=arguments;returnstr.replace(//{(/d+)/}/g,function(m,i){returnarg[parseInt(i)+1];});},each:function(object,callback,args){varname,i=0,length=object.length;if(args){if(length===undefined){for(nameinobject)if(callback.apply(object[name],args)===false)break;}elsefor(;i<length;)if(callback.apply(object[i++],args)===false)break;}else{if(length===undefined){for(nameinobject)if(callback.call(object[name],name,object[name])===false)break;}elsefor(varvalue=object[0];i<length&&callback.call(value,i,value)!==false;value=object[++i]){}}},setCookie:function(name,value,hours,path,domain){varstr=newString();varnextTime=newDate();nextTime.setHours(nextTime.getHours()+hours);str=name+"="+escape(value);if(hours)str+=";expires="+nextTime.toGMTString();if(path)str+=";path="+path;if(domain)str+=";domain="+domain;document.cookie=str;},getCookie:function(name){varrs=newRegExp("(^|)"+name+"=([^;]*)(;|$)","gi").exec(document.cookie),tmp;if(tmp=rs)returnunescape(tmp[2]);returnnull;},delCookie:function(name){document.cookie=name+"=-1"+";expires=Fri,31Dec199923:59:59GMT;";},/***urlString*parmsString*methodStringdefaultvalue"get"*asyBooleandefalutvaluetrue*successFunction(http_request.responseText)**/Ajax:function(config){varurl=config.url,parms=(config.parms?config.parms:"")+"&t="+newDate().getTime(),method=config.method||"get",asy=true;varhttp_request=null;if(method.toLowerCase()=="get"){url=url+"?"+parms;parms=null;}//开始初始化xmlHttPRequest对象if(window.xmlhttpRequest){//Mozilla浏览器http_request=newXMLHttpRequest();if(http_request.overrideMimeType){//设置MiME类别http_request.overrideMimeType("text/xml");}}elseif(window.ActiveXObject){//IE浏览器try{http_request=newActiveXObject("Msxml2.XMLHTTP");}catch(e){try{http_request=newActiveXObject("Microsoft.XMLHTTP");}catch(e){}}}if(!http_request){//异常,创建对象实例失败thrownewError("不能创建XMLHttpRequest对象实例.");returnnull;}http_request.open(method,url,asy);http_request.onreadystatechange=function(){if(http_request.readyState==4){try{if(http_request.status==200){config.success(http_request.responseText);}}catch(e){thrownewError("数据读取失败.");}}};if(method.toLowerCase()=="post"){http_request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");}http_request.send(parms);}};}();


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表