28个常用JavaScript方法集锦

时间:2021-05-28

经常使用的 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)),
}

复制代码 代码如下:
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 "";
}
  

复制代码 代码如下:
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);
  

复制代码 代码如下:
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;
}

复制代码 代码如下:
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;
}
  

复制代码 代码如下:
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://plete") {
if (typeof args.callback == "function") args.callback(this, args);
s.onload = s.onreadystatechange = null;
try {
s.parentNode && s.parentNode.removeChild(s);
} catch (e) { }
}
};
document.getElementsByTagName("head")[0].appendChild(s);
}
  

复制代码 代码如下:
function ClearHeadJs (src) {
var js = document.getElementsByTagName('head')[0].children;
var obj = null;
for (var i = 0; i < js.length; i++) {
if (js[i].tagName.toLowerCase() == "script" && js[i].attributes['src'].value.indexOf(src) > 0) {
obj = js[i];
}
}
document.getElementsByTagName('head')[0].removeChild(obj);
};
  

复制代码 代码如下:
function URLencode(sStr) {
return escape(sStr).replace(/\+/g, '%2B').replace(/\"/g, '%22').replace(/\'/g, '%27').replace(/\//g, '%2F');
};
  

复制代码 代码如下:
document.body.onkeydown = function (evt) {
evt = evt ? evt : (window.event ? window.event : null);
if (13 == evt.keyCode && evt.ctrlKey) {
evt.returnValue = false;
evt.cancel = true;
PostData();
}
};

  

复制代码 代码如下:
function GetCurrentDate() {
var d = new Date();
var y = d.getYear()+1900;
month = add_zero(d.getMonth() + 1),
days = add_zero(d.getDate()),
hours = add_zero(d.getHours());
minutes = add_zero(d.getMinutes()),
seconds = add_zero(d.getSeconds());
var str = y + '-' + month + '-' + days + ' ' + hours + ':' + minutes + ':' + seconds;
return str;
};
function add_zero(temp) {
if (temp < 10) return "0" + temp;
else return temp;
}

  

复制代码 代码如下:
String.prototype.Trim = function()
{ return this.replace(/(^\s*)|(\s*$)/g, ""); }
String.prototype.LTrim = function()
{return this.replace(/(^\s*)/g, "");}
String.prototype.RTrim = function()
{return this.replace(/(\s*$)/g, "");}

复制代码 代码如下:
this.ClearHeadJs = function (src) {
var js = document.getElementsByTagName('head')[0].children;
var obj = null;
for (var i = 0; i < js.length; i++) {
if (js[i].tagName.toLowerCase() == "script" && js[i].attributes['src'].value.indexOf(src) > 0) {
obj = js[i];
}
}
document.getElementsByTagName('head')[0].removeChild(obj);
};
  

复制代码 代码如下:
function CreateFrom(url, params) {
var f = document.createElement("form");
f.setAttribute("action", url);
for (var i = 0; i < params.length; i++) {
var input = document.createElement("input");
input.setAttribute("type", "hidden");
input.setAttribute("name", params[i].paramName);
input.setAttribute("value", params[i].paramValue);
f.appendChild(input);
}
f.target = "_blank";
document.body.appendChild(f);
f.submit();
};

复制代码 代码如下:
<script language="javascript">
var jsversion = 1.0;
</script>
<script language="javascript1.1">
jsversion = 1.1;
</script>
<script language="javascript1.2">
jsversion = 1.2;
</script>
<script language="javascript1.3">
jsversion = 1.3;
</script>
<script language="javascript1.4">
jsversion = 1.4;
</script>
<script language="javascript1.5">
jsversion = 1.5;
</script>
<script language="javascript1.6">
jsversion = 1.6;
</script>
<script language="javascript1.7">
jsversion = 1.7;
</script>
<script language="javascript1.8">
jsversion = 1.8;
</script>
<script language="javascript1.9">
jsversion = 1.9;
</script>
<script language="javascript2.0">
jsversion = 2.0;
</script>
alert(jsversion);

以上就是本文的全部内容了,希望能对大家有所帮助,也不枉整理这么久了,呵呵。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章