自己收集原生js-2014-2-23

时间:2023-12-18 14:32:20
(function(){})(
window.EventUtil={
addHandler:function(element,type,handler){
// alert(1);
if(element)
{
//alert(2);
if(element.addEventListener)
{
// alert(3);
element.addEventListener(type,handler,false);
}else if(element.attachEvent)
{
// alert(4);
element.attachEvent("on"+type,handler);
}else{
// alert(5);
element["on"+type]=handler;
} }
},
removeHandler:function(element,type,handler){
if(element)
{
if(element.removeEventListener){
element.removeEventListener(type,handler,false);
}else if(element.detachEvent){
element.detachEvent("on"+type,handler);
}else{
element["on"+type]=null;
}
} },
addReady: function(fn){
if(document.addEventListener){//兼容非IE
document.addEventListener("DOMContentLoaded",function(){
//注销事件,避免反复触发
document.removeEventListener("DOMContentLoaded",arguments.callee,false);
fn();//调用参数函数
},false);
}else if(document.attachEvent){//兼容IE
IEContentLoaded (window, fn);
}
function IEContentLoaded (w, fn) {
var d = w.document, done = false,
// only fire once
init = function () {
if (!done) {
done = true;
fn();
}
};
// polling for no errors
(function () {
try {
// throws errors until after ondocumentready
d.documentElement.doScroll('left');
} catch (e) {
setTimeout(arguments.callee, 50);
return;
}
// no errors, fire init();
})();
// trying to always fire before onload
d.onreadystatechange = function() {
if (d.readyState == 'complete') {
d.onreadystatechange = null;
init();
}
};
}
},
getEvent:function(event){
return event?window.event:event;
},
getEventTarget:function(event){
return event.target||event.srcElement;
},
preventDefault:function(event){
if(event.preventDefault){
event.preventDefault();
}else{
event.returnValue=false;
}
},
stopPropagation:function(event){
if(event.stopPropagation){
event.stopPropagation();
}
else{
event.cancelBubble=true;
} }, ajax:(function (){
function request (url,opt){
function createXHR(){
if(typeof XMLHttpRequest != "undefined"){
createXHR = function(){
return new XMLHttpRequest();
};
}else if(typeof ActiveXObject != "undefined"){
createXHR = function(){
if(typeof arguments.callee.activeXString != "string"){
var versions = ["MSXML2.XMLHttp.6.0","MSXML2.XMLHttp.3.0","MSXML2.XMLHttp"];
for(var i = 0, len = versions.length; i < len; i++){
try{
var xhr = new ActiveXObject(version[i]);
arguments.callee.activeXString = version[i];
return xhr;
}catch(ex){
//skip
}
}
}
return new ActiveXObject(arguments.callee.activeXString);
};
}else{
createXHR = function(){
throw new Error("No XHR Object available.");
};
}
return createXHR();
}
function fn(){}
var async=opt.async!==false,
method=opt.method||"Get",
data=opt.data||null,
success=opt.success||fn,
failure=opt.failure||fn;
method=method.toLocaleLowerCase();
if(method=="get"&&data){
url+=(url.indexOf('?')==-1?'?':'&')+data;
}
var xhr=createXHR();
xhr.onreadystatechange=function(){
_onStateChange(xhr,success,failure);
}
if(method=="post"){
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded;');
}
xhr.open(method,url,async);
xhr.send(data);
return xhr; }
function _onStateChange(xhr,success,failure){
if(xhr.readyState == 4){
var s = xhr.status;
if(s>= 200 && s < 300){
success(xhr);
}else{
failure(xhr);
}
}else{}
}
return {request:request}
})() }
);