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 < length; )
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 < length && callback.call( value, i, value ) !== false; value = object[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);
}
};
}();
posted on 2009-06-10 14:17
simone 阅读(2731)
评论(8) 所属分类:
javascript