더 이상 고민하지 않고 코드를 제시하고 무엇을 해야할지 알고 있다면 가져 가십시오.
코드 사본은 다음과 같습니다.
util = function () {
반품 {
$ : function (id) {
return document.getElementById (id);
},
트림 : 함수 (str) {
return str.replace (/(^/s+) | (/s+$)/g, "");
},
Len : 기능 (str) {
return str.replace (/[^/x00-/xff]/g, '**'). 길이;
},
형식 : 함수 (str) {
var arg = 인수;
return str.replace (// {(/d+)/}/g, function (m, i) {
arg arg [parseint (i) +1];
});
},
각각 : 함수 (Object, Callback, Args) {
var name, i = 0, 길이 = 객체 .length;
if (args) {
if (length === 정의되지 않은) {
for (객체의 이름)
if (callback.apply (개체 [이름], args) === 거짓)
부서지다;
} 또 다른
(; i <길이;)
if (callback.apply (Object [i ++], args) === 거짓)
부서지다;
} 또 다른 {
if (length === 정의되지 않은) {
for (객체의 이름)
if (callback.call (개체 [이름], 이름, 개체 [이름]) === 거짓)
부서지다;
} 또 다른
for (var value = Object [0];
i <longth && callback.call (value, i, value)! == false; value = Object [++ i]) {}
}
},
setCookie : 함수 (이름, 가치, 시간, 경로, 도메인) {
var str = new String ();
var nexttime = new Date ();
다음 time.sethours (다음 time.gethours ()+시간);
str = name+"="+탈출 (값);
if (시간)
str+= "; expires ="+nexttime.togmtstring ();
if (path)
str+= "; path ="+경로;
if (도메인)
str+= "; domain ="+도메인;
document.cookie = str;
},
getCookie : function (name) {
var rs = new regexp ( "(^|)"+name+"= ([^;]*) (; | $)", "gi"). exec (document.cookie), tmp;
if (tmp = rs)
반환 uncape (TMP [2]);
널 리턴;
},
delcookie : function (name) {
document.cookie = name + "= -1" + "; 만료 = Fri, 1999 년 12 월 31 일 23:59:59 GMT;";
},
/**
*URL 문자열
*파름 문자열
*메소드 문자열 기본값 "get"
*Asy Boolean defalut 가치 true
*성공 함수 (http_request.responsetext)
**/
ajax : 함수 (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 category를 설정합니다
http_request.overridemimeType ( "text/xml");
}
} else if (window.activexObject) {// IE 브라우저
노력하다 {
http_request = new ActiveXobject ( "msxml2.xmlhttp");
} catch (e) {
노력하다 {
http_request = new ActiveXobject ( "microsoft.xmlhttp");
} catch (e) {}
}
}
if (! http_request) {// 예외, 객체 인스턴스를 생성하지 못했습니다.
새 오류를 던지십시오 ( "xmlhttprequest 객체 인스턴스를 생성 할 수 없습니다.");
널 리턴;
}
http_request.open (method, url, asy);
http_request.onreadystatechange = function () {
if (http_request.readystate == 4) {
노력하다{
if (http_request.status == 200) {
config.success (http_request.responsetext);
}
} catch (e) {
새로운 오류를 던지십시오 ( "데이터 읽기 실패.");
}
}
};
if (method.tolowercase () == "post") {
http_request.setrequestheader ( "content-type", "application/x-www-form-urlencoded");
}
http_request.send (parms);
}
};
} ();
꽤 좋지 않습니까? 어쨌든 나는 매우 만족합니다.