经常使用的 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对象 */
function getEvent() { if (document.all) { return window.event; //如果是ie } func = getEvent.caller; while (func != null) { var arg0 = func.arguments[0]; if (arg0) { if ((arg0.constructor == Event || arg0.constructor == MouseEvent)|| (typeof (arg0) == "object" && arg0.preventDefault && arg0.stopPropagation)) { return arg0; } } func = func.caller; } return null;};
/*字符串截取方法 */
getCharactersLen: function (charStr, cutCount) { if (charStr == null || charStr == '') return ''; var totalCount = 0; var newStr = ''; for (var i = 0; i < charStr.length; i++) { var c = charStr.charCodeAt(i); if (c < 255 && c > 0) { totalCount++; } else { totalCount += 2; } if (totalCount >= cutCount) { newStr += charStr.charAt(i); break; } else { newStr += charStr.charAt(i); } } return newStr; }
/* JS 弹出新窗口全屏 */
var tmp = window.open("about:blank", "", "fullscreen=1") tmp.moveTo(0, 0); tmp.resizeTo(screen.width + 20, screen.height); tmp.focus(); tmp.location.href = 'http://www.che168.com/pinggu/eva_' + msgResult.message[0] + '.html';var config_ = "left=0,top=0,width=" + (window.screen.Width) + ",height=" + (window.screen.Height); window.open('http://www.che168.com/pinggu/eva_' + msgResult.message[0] + '.html', "winHanle", config_);//模拟form提交打开新页面var f = document.createElement("form"); f.setAttribute('action', 'http://www.che168.com/pinggu/eva_' + msgResult.message[0] + '.html'); f.target = '_blank'; document.body.appendChild(f); f.submit();
/*全选/全不选 */
function selectAll(objSelect) { if (objSelect.checked == true) { $("input[name='chkId']").attr("checked", true); $("input[name='chkAll']").attr("checked", true); } else if (objSelect.checked == false) { $("input[name='chkId']").attr("checked", false); $("input[name='chkAll']").attr("checked", false); } }
/* js 判断浏览器 */
判断是否是 IE 浏览器if (document.all){ alert(”IE浏览器”); }else{ alert(”非IE浏览器”); } if (!!window.ActiveXObject){ alert(”IE浏览器”); }else{ alert(”非IE浏览器”); } 判断是IE几var isIE=!!window.ActiveXObject; var isIE6=isIE&&!window.xmlHttpRequest; var isIE8=isIE&&!!document.documentMode; var isIE7=isIE&&!isIE6&&!isIE8; if (isIE){ if (isIE6){ alert(”ie6″); }else if (isIE8){ alert(”ie8″); }else if (isIE7){ alert(”ie7″); } }
/* 判断浏览器 */
function getOs() { if (navigator.userAgent.indexOf("MSIE 8.0") > 0) { return "MSIE8"; } else if (navigator.userAgent.indexOf("MSIE 6.0") > 0) { return "MSIE6"; } else if (navigator.userAgent.indexOf("MSIE 7.0") > 0) { return "MSIE7"; } else if (isFirefox = navigator.userAgent.indexOf("Firefox") > 0) { return "Firefox"; } if (navigator.userAgent.indexOf("Chrome") > 0) { return "Chrome"; } else { return "Other"; }}
/* JS判断两个日期大小 适合 2012-09-09 与2012-9-9 两种格式的对比 */
//得到日期值并转化成日期格式,replace(//-/g, "//")是根据验证表达式把日期转化成长日期格式,这样再进行判断就好判断了 function ValidateDate() { var beginDate = $("#t_datestart").val(); var endDate = $("#t_dateend").val(); if (beginDate.length > 0 && endDate.length>0) { var sDate = new Date(beginDate.replace(//-/g, "//")); var eDate= new Date(endDate.replace(//-/g, "//")); if (sDate > eDate) { alert('开始日期要小于结束日期'); return false; } } }
/* 移除事件 */
this.moveBind = function (objId, eventType, callBack) { var obj = document.getElementById(objId); if (obj.removeEventListener) { obj.removeEventListener(eventType,
新闻热点
疑难解答