分享一个常用的javascript静态类_javascript技巧
程序员文章站
2022-03-22 10:48:21
...
废话不多说,直接奉上代码,知道干啥用的小伙伴直接带走吧。
util=function(){
return {
$:function(id){
return document.getElementById(id);
},
trim:function(str){
return str.replace(/(^\s+)|(\s+$)/g, "");
},
len:function(str){
return str.replace(/[^\x00-\xff]/g,'**').length;
},
format:function(str){
var arg = arguments;
return str.replace(/\{(\d+)\}/g, function(m, i){
return arg[parseInt(i)+1];
});
},
each:function(object, callback, args){
var name, i = 0, length = object.length;
if ( args ) {
if ( length === undefined ) {
for ( name in object )
if ( callback.apply( object[ name ], args ) === false )
break;
} else
for ( ; i if ( callback.apply( object[ i++ ], args ) === false )
break;
} else {
if ( length === undefined ) {
for ( name in object )
if ( callback.call( object[ name ], name, object[ name ] ) === false )
break;
} else
for ( var value = object[0];
i }
},
setCookie:function(name,value,hours,path,domain){
var str=new String();
var nextTime=new Date();
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){
var rs=new RegExp("(^|)"+name+"=([^;]*)(;|$)","gi").exec(document.cookie),tmp;
if(tmp=rs)
return unescape(tmp[2]);
return null;
},
delCookie:function(name){
document.cookie = name + "=-1" + "; expires=Fri, 31 Dec 1999 23:59:59 GMT;";
},
/**
*url String
*parms String
*method String default value "get"
*asy Boolean defalut value true
*success Function(http_request.responseText)
**/
ajax:function(config){
var url=config.url,
parms=(config.parms?config.parms:"") + "&t="+new Date().getTime(),
method=config.method||"get",
asy=true;
var http_request=null;
if(method.toLowerCase()=="get"){
url=url+"?"+parms;
parms=null;
}
//开始初始化XMLHttpRequest对象
if(window.XMLHttpRequest) { //Mozilla 浏览器
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) {//设置MiME类别
http_request.overrideMimeType("text/xml");
}
} else if (window.ActiveXObject) { // IE浏览器
try {
http_request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if(!http_request) { // 异常,创建对象实例失败
throw new Error("不能创建XMLHttpRequest对象实例.");
return null;
}
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){
throw new Error("数据读取失败.");
}
}
};
if(method.toLowerCase()=="post"){
http_request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
}
http_request.send(parms);
}
};
}();
复制代码 代码如下:
util=function(){
return {
$:function(id){
return document.getElementById(id);
},
trim:function(str){
return str.replace(/(^\s+)|(\s+$)/g, "");
},
len:function(str){
return str.replace(/[^\x00-\xff]/g,'**').length;
},
format:function(str){
var arg = arguments;
return str.replace(/\{(\d+)\}/g, function(m, i){
return arg[parseInt(i)+1];
});
},
each:function(object, callback, args){
var name, i = 0, length = object.length;
if ( args ) {
if ( length === undefined ) {
for ( name in object )
if ( callback.apply( object[ name ], args ) === false )
break;
} else
for ( ; i if ( callback.apply( object[ i++ ], args ) === false )
break;
} else {
if ( length === undefined ) {
for ( name in object )
if ( callback.call( object[ name ], name, object[ name ] ) === false )
break;
} else
for ( var value = object[0];
i }
},
setCookie:function(name,value,hours,path,domain){
var str=new String();
var nextTime=new Date();
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){
var rs=new RegExp("(^|)"+name+"=([^;]*)(;|$)","gi").exec(document.cookie),tmp;
if(tmp=rs)
return unescape(tmp[2]);
return null;
},
delCookie:function(name){
document.cookie = name + "=-1" + "; expires=Fri, 31 Dec 1999 23:59:59 GMT;";
},
/**
*url String
*parms String
*method String default value "get"
*asy Boolean defalut value true
*success Function(http_request.responseText)
**/
ajax:function(config){
var url=config.url,
parms=(config.parms?config.parms:"") + "&t="+new Date().getTime(),
method=config.method||"get",
asy=true;
var http_request=null;
if(method.toLowerCase()=="get"){
url=url+"?"+parms;
parms=null;
}
//开始初始化XMLHttpRequest对象
if(window.XMLHttpRequest) { //Mozilla 浏览器
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) {//设置MiME类别
http_request.overrideMimeType("text/xml");
}
} else if (window.ActiveXObject) { // IE浏览器
try {
http_request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if(!http_request) { // 异常,创建对象实例失败
throw new Error("不能创建XMLHttpRequest对象实例.");
return null;
}
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){
throw new Error("数据读取失败.");
}
}
};
if(method.toLowerCase()=="post"){
http_request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
}
http_request.send(parms);
}
};
}();
是不是相当不错呢,反正我是很满意。
推荐阅读
-
js+csss实现的一个带复选框的下拉框_javascript技巧
-
JavaScript实现防止网页被嵌入Frame框架的代码分享_javascript技巧
-
JavaScript中实现依赖注入的思路分享_javascript技巧
-
日常收集整理的JavaScript常用函数方法_javascript技巧
-
探索Emberjs制作一个简单的Todo应用_javascript技巧
-
非常不错的一个javascript 类_javascript技巧
-
基于JavaScript操作DOM常用的API小结_javascript技巧
-
7个好用的JavaScript技巧分享(译)
-
分享5个小技巧让你写出更好的 JavaScript 条件语句
-
JavaScript开发中14个有用的Jquery技巧分享