A summary of common JavaScript methods

Source: Internet
Author: User
Tags tagname

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 increase in expected count
Else
a++;
}
return A;
}
Get the parameters in the 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;
}

This article shared before I still want to introduce my own front-end group: 657137906, whether you are small white is still Daniel, small series I look forward to, not regularly share dry goods, including me from a collation of a 2017 of the latest front-end materials and 0 basic introductory tutorials, look forward to beginners and advanced small partners.

function GetEvent () {
if (document.all) {
return window.event; If it is 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 +, screen.height);
Tmp.focus ();
Tmp.location.href = '//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 ('//www.che168.com/pinggu/eva_ ' + msgresult.message[0] + '. html ', "Winhanle", config_);
Simulate form submission open a new page
var f = document.createelement ("form");
F.setattribute (' action ', '//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);
}
}


Determine if it is IE browser
if (document.all) {
Alert ("IE browser");
}else{
Alert ("Non-IE browser");
}
if (!! Window. ActiveXObject) {
Alert ("IE browser");
}else{
Alert ("Non-IE browser");
}
The judgment is IE several
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";
}
}


To get the date value and convert it into a date format, replace (/-/g, "/") is converted to a date format based on the validation expression, so that you can judge the
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 (' Start date less than End Date ');
return false;
}
}
}


This.movebind = function (ObjId, EventType, CallBack) {
var obj = document.getElementById (objId);
if (Obj.removeeventlistener) {
Obj.removeeventlistener (EventType, CallBack, false);
}
else if (window.detachevent) {
Obj.detachevent (' on ' + EventType, callBack);
}
else {
obj[' on ' + eventtype] = null;
}
}


$ ("id"). onkeypress = function (event) {
Event = (event)? Event: ((window.event)? Window.event: "")
KeyCode = Event.keycode? Event.keycode: (Event.which event.which:event.charCode);
if (keycode = = 13) {
$ ("Submitlogin"). OnClick ();
}
}


Timestart = new Date (). GetTime ();
Timesend = new Date (). GetTime ();
document.getElementById ("Time"). InnerHTML = Timesend-timestart;


function Setcookie (name, value, expires, path, domain) {
if (!expires) expires =-1;
if (!path) path = "/";
var d = "" + name + "=" + value;
var e;
if (expires < 0) {
e = "";
}
else if (expires = = 0) {
var f = new Date (1970, 1, 1);
e = "; expires=" + f.toutcstring ();
}
else {
var now = new Date ();
var f = new Date (Now.gettime () + expires * 1000);
e = "; expires=" + f.toutcstring ();
}
var DM;
if (!domain) {
DM = "";
}
else {
DM = ";d omain=" + domain;
}
Document.cookie = name + "=" + Value + ";p ath=" + path + e + DM;
};


function Readcookie (name) {
var Nameeq = name + "=";
var ca = Document.cookie.split (';');
for (var i = 0; i < ca.length; i++) {
var c = Ca[i];
while (C.charat (0) = = ") c = c.substring (1, c.length);
if (C.indexof (nameeq) = = 0) {
Return decodeURIComponent (c.substring (Nameeq.length, C.length))
}
} return NULL
}


C.ajax = function (args) {
var = this;
This.options = {
Type: ' GET ',
Async:true,
ContentType: ' application/x-www-form-urlencoded ',
URL: ' About:blank ',
Data:null,
Success: {},
Error: {}
};
This.getxmlhttp = function () {
var xmlHttp;
try {
XMLHTTP = new XMLHttpRequest ();
}
catch (e) {
try {
XMLHTTP = new ActiveXObject ("Msxml2.xmlhttp");
}
catch (e) {
XmlHttp = new ActiveXObject ("Microsoft.XMLHTTP");
}
}
if (!xmlhttp) {
Alert (' Your browser does not support Ajax ');
return false;
}
return XMLHTTP;
};
This.send = function () {
C.each (self.options, Function (key, Val) {
Self.options[key] = (Args[key] = = null)? Val:args[key];
});
var xmlHttp = new Self.getxmlhttp ();
if (self.options.type.toUpperCase () = = ' GET ') {
Xmlhttp.open (Self.options.type, Self.options.url + (Self.options.data = = null?) "" : ((/[?] $/.test (Self.options.url)? ' & ': '? ') + Self.options.data)), Self.options.async);
}
else {
Xmlhttp.open (Self.options.type, Self.options.url, Self.options.async);
Xmlhttp.setrequestheader (' Content-length ', self.options.data.length);
}
Xmlhttp.setrequestheader (' Content-type ', self.options.contentType);
Xmlhttp.onreadystatechange = function () {
if (xmlhttp.readystate = = 4) {
if (xmlhttp.status = = | | xmlhttp.status = = 0) {
if (typeof self.options.success = = ' function ') self.options.success (Xmlhttp.responsetext);
XmlHttp = null;
}
else {
if (typeof self.options.error = = ' function ') self.options.error (' Server Status: ' + xmlhttp.status);
}
}
};
Xmlhttp.send (self.options.type.toUpperCase () = = ' POST '? self.options.data.toString (): null);
};
This.send ();
};


function StringBuilder () {
This.strings = new Array;
};
StringBuilder.prototype.append = function (str) {
This.strings.push (str);
};
StringBuilder.prototype.toString = function () {
Return This.strings.join (");
};


function Loadjs (URL, fn) {
var ss = document.getelementsbyname (' script '),
loaded = false;
for (var i = 0, len = ss.length; i < Len; i++) {
if (ss[i].src && ss[i].getattribute (' src ') = = URL) {
loaded = true;
Break
}
}
if (loaded) {
if (fn && typeof fn! = ' undefined ' && fn instanceof Function) fn ();
return false;
}
var s = document.createelement (' script '),
b = false;
S.setattribute (' type ', ' text/javascript ');
S.setattribute (' src ', url);
S.onload = S.onreadystatechange = function () {
if (!b && (!this.readystate | | this.readystate = = ' Loaded ' | | this.readystate = = ' complete ')} {
B = true;
if (fn && typeof fn! = ' undefined ' && fn instanceof Function) fn ();
}
};
document.getElementsByTagName (' head ') [0].appendchild (s);
},
Bind:function (ObjId, EventType, callBack) {//bindings for any browser
var obj = document.getElementById (objId);
if (Obj.addeventlistener) {
Obj.addeventlistener (EventType, CallBack, false);
}
else if (window.attachevent) {
Obj.attachevent (' on ' + EventType, callBack);
}
else {
obj[' on ' + eventtype] = CallBack;
}
}
function Jsload (args) {
s = document.createelement ("script");
S.setattribute ("type", "Text/javascript");
S.setattribute ("src", args.url);
S.onload = S.onreadystatechange = function () {
if (!s.readystate | | s.readystate = = "Loaded" | | s.readystate = = "complete") {
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 (= = 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 <) 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);

A summary of common JavaScript methods

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.