常用的几个js函数

 

 1 /**
 2  * 事件绑定
 3  */
 4 function bindEvent(target, noOnEventName, handler, useCapture) {
 5     useCapture = useCapture || false;
 6 
 7     if (window.addEventListener) {
 8         target.addEventListener(noOnEventName, handler, useCapture);
 9     } else if (window.attachEvent) {
10         // IE
11         target.attachEvent("on" + noOnEventName, handler);
12     } else {
13         target["on" + noOnEventName] = handler;
14     }
15 };
16 
17 
18 /**
19  * 事件移除
20  */
21 function removeEvent(target, noOnEventName, handler, useCapture){
22     useCapture = useCapture || false;
23 
24     if (window.removeEventListener) {
25         target.removeEventListener(noOnEventName, handler, useCapture);
26     } else if (window.detachEvent) {
27         // IE
28         target.detachEvent("on" + noOnEventName, handler);
29     } else {
30         delete target["on" + noOnEventName];
31     }
32 }
事件绑定/移除
 1 /**
 2  * 获取URL参数
 3  */
 4 function GetUrlParam(name) {
 5         var query = location.search.substring(1);//获取查询串   
 6         var pairs = query.split("&");//在逗号处断开   
 7         for (var i = 0; i < pairs.length; i++) {
 8             var pos = pairs[i].indexOf('=');//查找name=value   
 9             if (pos == -1)//如果没有找到就跳过  
10                 continue;
11             var argname = pairs[i].substring(0, pos);//提取name   
12             var value = pairs[i].substring(pos + 1);//提取value   
13             if (argname.toLowerCase() == name.toLowerCase())
14                 return decodeURIComponent(value);
15         }
16         return "";
17     }
获取URL参数
 1 /**
 2  * 动态加载js
 3  */
 4 function jsLoad(url, func) {
 5     var script = document.createElement("script");
 6     script.src = url;
 7     script.charset = "utf-8";
 8     script.type = "text/javascript";
 9 
10     var head = window.document.getElementsByTagName("head").item(0);
11     head.appendChild(script);
12 
13     script.onload = script.onreadystatechange = function () {
14         if (!this.readyState || this.readyState == 'loaded' || this.readyState == 'complete') {
15             if (func) {
16                 func();
17             }
18         }
19         script.onload = script.onreadystatechange = null;
20     };
21 }
22 
23 
24 /**
25  * 动态加载css
26  */
27 function cssLoad(url, func) {
28     var css = document.createElement("link");
29     css.href = url;
30     css.rel = 'stylesheet';
31     css.type = 'text/css';
32 
33     var head = window.document.getElementsByTagName("head").item(0);
34     head.appendChild(css);
35 
36     css.onload = css.onreadystatechange = function () {
37         if (!this.readyState || this.readyState == 'loaded' || this.readyState == 'complete') {
38             if (func) {
39                 func();
40             }
41         }
42         css.onload = css.onreadystatechange = null;
43     };
44 }
45 
46 
47 /**
48  * 移除css
49  */
50 function removeCss(url) {
51     var targetAttr = "href";
52     var allCss = document.getElementsByTagName("link");
53     for (var i = 0; i < allCss.length; i++) {
54         if (allCss[i].getAttribute(targetAttr) == url) {
55             allCss[i].parentNode.removeChild(allCss[i]);
56             break;
57         }
58     }
59 }
动态加载js/css
 1 /**
 2  * 停止事件冒泡
 3  */
 4 function stopBubble(e) {
 5     //如果提供了事件对象,则这是一个非IE浏览器
 6     if (e && e.stopPropagation)
 7         //因此它支持W3C的stopPropagation()方法
 8         e.stopPropagation();
 9     else
10         //否则,我们需要使用IE的方式来取消事件冒泡
11         window.event.cancelBubble = true;
12 }
13 
14 
15 /**
16  * 阻止浏览器的默认行为
17  */
18 function stopDefault(e) {
19     //阻止默认浏览器动作(W3C)
20     if (e && e.preventDefault)
21         e.preventDefault();        
22     else
23         //IE中阻止函数器默认动作的方式
24         window.event.returnValue = false;
25     return false;
26 }        
阻止冒泡/默认事件
 1 /**
 2  * 键盘事件
 3  */
 4 function onKeyDown(e) {
 5     var keyCode;
 6     if (window.event) {
 7         keyCode = event.keyCode;
 8     } else {
 9         keyCode = e.which;
10     }
11 
12     //回车
13     if (keyCode == 13) {
14         //todo do something
15     }
16 
17     //后退
18     if (keyCode == 8) {
19         //阻止浏览器的默认行为
20         stopDefault(e);
21 
22         //todo do something
23     }
24 }
键盘事件
 1 /**
 2  * IE9跨域GET请求(服务器已设置允许跨域)
 3  */
 4 function ie9Get(url, funcSucc, funcErr) {
 5     var xdr = new XDomainRequest();
 6     xdr.onload = function (e) {
 7         if (typeof funcSucc == "function") {
 8             funcSucc(xdr.responseText);
 9         }
10     };
11     xdr.onerror = function (e) {
12         if (typeof funcErr == "function") {
13             funcErr(e);
14         }
15     };
16     xdr.open("GET", url);
17     xdr.send();
18 }
IE9跨域GET请求
 1 /**
 2  * 当前鼠标坐标
 3  */
 4 function mouseCoordinates(e) {
 5     if (e.pageX || e.pageY) {
 6         return {
 7             x: e.pageX,
 8             y: e.pageY
 9         };
10     }
11     return {
12         x: e.clientX + document.body.scrollLeft - document.body.clientLeft,
13         y: e.clientY + document.body.scrollTop - document.body.clientTop
14     };
15 }
鼠标坐标
 1 var Util = {
 2     random: function (max) {
 3         return Math.floor(Math.random() * max);
 4     },
 5     isEmptyObject: function (obj) {
 6         for (var n in obj) {
 7             if (obj.hasOwnProperty(n))
 8                 return false;
 9         }
10         return true;
11     },
12     trim: function (s) {
13         return s.replace(/(^\s*)|(\s*$)|(\n)/g, "");
14     },
15     leftTrim: function (s) {
16         return s.replace(/(^\s*)|(^\n)/g, "");
17     },
18     rightTrim: function (s) {
19         return s.replace(/(\s*$)|(\n$)/g, "");
20     },
21     isString: function (s) {
22         var p = /^([a-z]|[A-Z])+$/;
23         return p.exec(s);
24     },
25     isNumber: function (s) {
26         var p = /^\d+\.\d+$/;
27         return p.exec(s);
28     },
29     isInt: function (s) {
30         var p = /^\d+$/;
31         return p.exec(s);
32     },
33     isFunction: function (value) {
34         return typeof value == 'function';
35     },
36     isArray: function (value) {
37         return toString.apply(value) == '[object Array]';
38     },
39     isBoolean: function (value) {
40         return typeof value == 'boolean';
41     },
42     isObject: function (value) {
43         return value != null && typeof value == 'object';
44     },
45     isDate: function (value) {
46         return toString.apply(value) == '[object Date]';
47     }
48 };
其他方法
 1 String.prototype.trim = function () {
 2     return this.replace(/^s+|s+$/g, '');
 3 };
 4 
 5 String.prototype.ltrim = function () {
 6     return this.replace(/^s+/g, '');
 7 };
 8 
 9 String.prototype.rtrim, function () {
10     return this.replace(/s+$/g, '');
11 };
12 
13 Array.prototype.clear = function () {
14     this.length = 0;
15 };
16 
17 Array.prototype.insertAt = function (index, obj) {
18     this.splice(index, 0, obj);
19 };
20 
21 Array.prototype.removeAt = function (index) {
22     this.splice(index, 1);
23 };
24 
25 Array.prototype.indexOf = function (item) {
26     for (var i = 0; i < this.length; i++) {
27         if (this[i] == item)
28             return i;
29     }
30     return -1;
31 };
32 
33 if (!Date.now) {
34     Date.now = function () {
35         return (new this()).valueOf();
36     }
37 }
基本扩展
 1 (function(){
 2     var Base64 = {
 3         encode : function(str){
 4             return window.btoa(unescape(encodeURIComponent(str)));
 5         },
 6         decode : function(str){
 7             return decodeURIComponent(escape(window.atob(str)));
 8         }
 9     };
10     window.BASE64 = Base64;
11 })();
12 
13 
14 (function() {
15     if (!window.btoa) {
16         var a = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
17         window.btoa = function(c) {
18             var d = "";
19             var m, k, h = "";
20             var l, j, g, f = "";
21             var e = 0;
22             do {
23                 m = c.charCodeAt(e++);
24                 k = c.charCodeAt(e++);
25                 h = c.charCodeAt(e++);
26                 l = m >> 2;
27                 j = ((m & 3) << 4) | (k >> 4);
28                 g = ((k & 15) << 2) | (h >> 6);
29                 f = h & 63;
30                 if (isNaN(k)) {
31                     g = f = 64
32                 } else {
33                     if (isNaN(h)) {
34                         f = 64
35                     }
36                 }
37                 d = d + a.charAt(l) + a.charAt(j) + a.charAt(g) + a.charAt(f);
38                 m = k = h = "";
39                 l = j = g = f = ""
40             } while (e < c.length);
41             return d
42         };
43         window.atob = function(c) {
44             var d = "";
45             var m, k, h = "";
46             var l, j, g, f = "";
47             var e = 0;
48             do {
49                 l = a.indexOf(c.charAt(e++));
50                 if (l < 0) {
51                     continue
52                 }
53                 j = a.indexOf(c.charAt(e++));
54                 if (j < 0) {
55                     continue
56                 }
57                 g = a.indexOf(c.charAt(e++));
58                 if (g < 0) {
59                     continue
60                 }
61                 f = a.indexOf(c.charAt(e++));
62                 if (f < 0) {
63                     continue
64                 }
65                 m = (l << 2) | (j >> 4);
66                 k = ((j & 15) << 4) | (g >> 2);
67                 h = ((g & 3) << 6) | f;
68                 d += String.fromCharCode(m);
69                 if (g != 64) {
70                     d += String.fromCharCode(k)
71                 }
72                 if (f != 64) {
73                     d += String.fromCharCode(h)
74                 }
75                 m = k = h = "";
76                 l = j = g = f = ""
77             } while (e < c.length);
78             return d
79         }
80     }
81     var b = {
82         encode: function(c) {
83             return window.btoa(unescape(encodeURIComponent(c)))
84         },
85         decode: function(c) {
86             return decodeURIComponent(escape(window.atob(c)))
87         }
88     };
89     window.BASE64 = b
90 })();
Base64

 

posted on 2014-07-22 13:40  Darex  阅读(221)  评论(0)    收藏  举报