JS some common methods

Source: Internet
Author: User

String adds IsNullOrEmpty:

String.prototype.IsNullOrEmpty = function (r) {
if (r = = = Undefined | | r = = NULL | | r = = = "") {
return true;
} else {
return false;
}
}

JSON to string:

function Jsontostring (o) {
var arr = [];
var FMT = function (s) {
if (typeof s = = ' object ' && s! = null) return jsontostr (s);
Return/^ (String|number) $/.test (typeof s)? "'" + S + "'": s;
}
for (var i in O) arr.push ("'" + i + "':" + FMT (O[i]));
Return ' {' + arr.join (', ') + '} ';
}

Get Browser version:

function GetExplorer () {
var osobject = "";
if (Navigator.userAgent.indexOf ("MSIE") > 0) {
return "IE";
}
if (Isfirefox = navigator.userAgent.indexOf ("Firefox") > 0) {
return "Firefox";
}
if (Issafari = navigator.userAgent.indexOf ("Safari") > 0) {
return "Safari";
}
if (Iscamino = navigator.userAgent.indexOf ("Camino") > 0) {
return "Camino";
}
if (Ismozilla = navigator.userAgent.indexOf ("gecko/") > 0) {
return "Gecko";
}
}

Some ways to manipulate dates:

function Formatjsontime (JSON) {
if (JSON! = null) {
var date = Convertjsondatetojsdateobject (JSON);
return GetTime (date);
}
else {
Return "";
}
}
function Formatjsondate (JSON) {
if (JSON! = null) {
var date = Convertjsondatetojsdateobject (JSON);
return GetDateTime (date);
}
else {
Return "";
}
}

function Formatjsonday (JSON) {
if (JSON! = null) {
var date = Convertjsondatetojsdateobject (JSON);
return GetDate (date);
}
else {
Return "";
}
}

/* JSON time converted to JS time object */
function Convertjsondatetojsdateobject (jsondate) {
var date = new Date (parseint ("Jsondate.replace" ("/date", "" "). Replace (")/"," "), 10));
return date;
}

/* Get date */
function GetDate (date) {
var year = Date.getfullyear ();
var month = Date.getmonth () + 1;
var day = Date.getdate ();
Return year + "-" + month + "-" + day;
}

/* Get date */
function Getdate_ (date) {
var year = Date.getfullyear ();
var month = Date.getmonth () + 1;
var day = Date.getdate ();
Return year + "/" + month + "/" + day;
}

/* Get Date Time */
function GetDateTime (date) {
var year = Date.getfullyear ();
var month = Date.getmonth () + 1;
var day = Date.getdate ();
var hh = date.gethours ();
var mm = Date.getminutes ();
var ss = Date.getseconds ();
Return year + "-" + month + "-" + Day + "" + hh + ":" + mm + ":" + SS;
}
function GetTime (date) {
var hh = date.gethours ();
var mm = Date.getminutes ();
var ss = Date.getseconds ();
return hh + ":" + mm + ":" + SS;
}

JS generates UUID

function Str4 () {
Return ((1 + math.random ()) * 0x10000) | 0). toString (+). substring (1);
}
function UUID () {
return STR4 () + STR4 () + "-" + STR4 () + "-" + STR4 () + "-" + STR4 () + "-" + STR4 () + STR4 () + STR4 ();
}

Get URL parameters
function Request (paras) {
var url = location.href;
var parastring = url.substring (Url.indexof ("?") + 1, url.length). Split ("&");
var paraobj = {}
for (i = 0; j = parastring[i]; i++) {
Paraobj[j.substring (0, j.indexof ("=")). toLowerCase ()] = j.substring (j.indexof ("=") + 1, j.length);
}
var returnvalue = Paraobj[paras.tolowercase ()];
if (typeof (returnvalue) = = "undefined") {
Return "";
} else {
Return returnvalue;
}
}

Array Increment indexof Property

function Checkindexof () {
if (! ARRAY.PROTOTYPE.INDEXOF) {
Array.prototype.indexOf = function (ELT/*, from*/) {
var len = this.length >>> 0;

var from = number (Arguments[1]) | | 0;
From = (From < 0)
? Math.ceil (from)
: Math.floor (from);
if (from < 0)
from + = Len;

for (; from < Len; from++) {
if (from in this &&
This[from] = = = ELT)
return from;
}
return-1;
};
}
}

Window Open

Screenh = Window.screen.availHeight;
Screenw = Window.screen.availWidth
function Openwin (Winurl, Cwidth, cheight) {
var height = Cheight = = null? 400:cheight;
var width = Cwidth = = null? 400:cwidth;
var pos_left = (screenw-width)/2;
var pos_top = (screenh-height)/2;
var Winid = STR4 ();
window.open (Winurl, Winid, ' height= ' + height + ', width= ' + width + ', top= ' + Pos_top + ', left= ' + Pos_left + ', Toolba R=no, Menubar=no, Scrollbars=yes, Resizable=yes,location=no, Status=no ');
}

JS some common methods

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.