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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
(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}
        })()
 
        }
);

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。