经常使用的 JS 方法,今天记下,以便以后查询
/* 手机类型判断 */
复制代码 代码如下:
var BrowserInfo = {
userAgent: navigator.userAgent.toLowerCase()
isAndroid: Boolean(navigator.userAgent.match(/android/ig)),
isIphone: Boolean(navigator.userAgent.match(/iphone|ipod/ig)),
isIpad: Boolean(navigator.userAgent.match(/ipad/ig)),
isWeixin: Boolean(navigator.userAgent.match(/MicroMessenger/ig)),
}
/* 返回字符串长度,汉子计数为2 */
复制代码 代码如下:
function strLength(str) {
var a = 0;
for (var i = 0; i < str.length; i++) {
if (str.charCodeAt(i) > 255)
a += 2;//按照预期计数增加2
else
a++;
}
return a;
}
获取url中的参数
复制代码 代码如下:
function GetQueryStringRegExp(name,url) {
var reg = new RegExp("(^|\\?|&)" + name + "=([^&]*)(\\s|&|$)", "i");
if (reg.test(url)) return decodeURIComponent(RegExp.$2.replace(/\+/g, " ")); return "";
}
/* js 绑定事件 适用于任何浏览器的元素绑定 */
复制代码 代码如下:
function eventBind(obj, eventType, callBack) {
if (obj.addEventListener) {
obj.addEventListener(eventType, callBack, false);
}
else if (window.attachEvent) {
obj.attachEvent('on' + eventType, callBack);
}
else {
obj['on' + eventType] = callBack;
}
};
eventBind(document, 'click', bodyClick);
/* 获得当前浏览器JS的版本 */
复制代码 代码如下:
function getjsversion(){
var n = navigator;
var u = n.userAgent;
var apn = n.appName;
var v = n.appVersion;
var ie = v.indexOf('MSIE ');
if (ie > 0){
apv = parseInt(i = v.substring(ie + 5));
if (apv > 3) {
apv = parseFloat(i);
}
} else {
apv = parseFloat(v);
}
var isie = (apn == 'Microsoft Internet Explorer');
var ismac = (u.indexOf('Mac') >= 0);
var javascriptVersion = "1.0";
if (String && String.prototype) {
javascriptVersion = '1.1';
if (javascriptVersion.match) {
javascriptVersion = '1.2';
var tm = new Date;
if (tm.setUTCDate) {
javascriptVersion = '1.3';
if (isie && ismac && apv >= 5) javascriptVersion = '1.4';
var pn = 0;
if (pn.toPrecision) {
javascriptVersion = '1.5';
a = new Array;
if (a.forEach) {
javascriptVersion = '1.6';
i = 0;
o = new Object;
tcf = new Function('o', 'var e,i=0;try{i=new Iterator(o)}catch(e){}return i');
i = tcf(o);
if (i && i.next) {
javascriptVersion = '1.7';
}
}
}
}
}
}
return javascriptVersion;
}
/* 获取当前点击事件的Object对象 */
复制代码 代码如下: