JS Tool Class

Source: Internet
Author: User
Tags id3

When writing JavaScript code, you often need tools and public methods, which gather common common methods and encapsulate them as tool classes.

Date Tool Class
/********************** Date Tool Class ***************/date.prototype.format = function (format) {    var o = {        "m+": This.getmonth () +1,//month        "d+": This.getdate (),//day        "H +": this.gethours (),//hour        "m+": This.getminutes (),//minute        "s+": This.getseconds (),//second        "q+": Math.floor ((This.getmonth () +3)/3),// Quarter        "S": This.getmilliseconds ()//millisecond    }    if (/(y+)/.test (format)) Format=format.replace ( Regexp.$1, (this.getfullyear () + ""). substr (4-regexp.$1.length));    For (var k in O) if (New RegExp ("(" + K + ")"). Test (format))    format = Format.replace (regexp.$1,regexp.$1.length==1? o[k ]: ("XX" + o[k]). substr (("" + o[k]). length);    return format;};                                                            
Public Tools Class

Empty the contents of the input box, the textarea, can be directly $("textarea").empty(); if used $("textarea").html(""); may also cause browser memory overflow.

/********************** Public Tools Class ***************/var Publicutil ={isnotempty:function (val) {return!this.isempty    (Val); }, Isempty:function (val) {if ((Val==null | | typeof (val) = = "undefined") | | (typeof (val) = = "string" &&val== "" &&val!= "undefined"))        {return true;        }else{return false; }}, Isdebug:function () {if (This.isnotempty (configdebug) &&configdebug== "true") {return TR        Ue        }else{return false; }},//Remove all content within the element strids: "#id1, #id2, #id3" Emptyhtml:function (strids) {try{var ids = Strids.tri            M (","). Split (",");                $ (IDS). each (function () {var obj = $ (this.tostring ());                    if (obj.length>0) {$ (obj). each (function () {$ (this). html ("");                });                }else{obj.html ("");        }            });      }catch (ex) {      if (Publicutil.isdebug ()) {throw new Error ("JS method:" Publicutil.emptyhtml (Strids) ", error!            "); }}},//Remove the value of the element strids: "#id1, #id2, #id3" Emptyvalue:function (strids) {try{var ids = str            Ids.trim (","). Split (",");                $ (IDS). each (function () {var obj = $ (this.tostring ());                    if (obj.length>0) {$ (obj). each (function () {$ (this). Val ("");                });                }else{obj.val ("");        }            }); }catch (ex) {if (Publicutil.isdebug ()) {throw new Error ("JS method:" Publicutil.emptyvalue (Strids) ", Erro R!            "); }}},//Remove all content within textarea Strids: "#id1, #id2, #id3" Emptytextarea:function (strids) {try{V            Ar ids = Strids.trim (","). Split (",");                $ (IDS). each (function () {var obj = $ (this.tostring ());  if (obj.length>0) {                  $ (obj). each (function () {$ (this). empty ();                    $ (this). Val ("");                });                    }else{Obj.empty ();                Obj.val ("");        }            }); }catch (ex) {if (Publicutil.isdebug ()) {throw new Error ("JS method:" Publicutil.emptytextarea (Strids) ", E rror!            ");                                                            }        }    }}
string Tool class

The concatenation of the string must use StringBuffer to splice, otherwise easy to cause the browser lag or memory overflow. Especially for some of the implementation of JS inefficient browser!!

/********************** string Tool class ***************///trim removes the specified characters on either side of the string, String.prototype.trim = function (tag) {if (    !tag) {tag = ' \\s ';     }else {if (tag = = ' \ \ ') {tag = ' \\\\ '; } else if (tag = = ', ' | | | tag = = ' | ' | | | tag = = '; ')         {tag = ' \ \ ' + tag;         }else {tag = ' \\s ';     }} eval (' var reg=/(^ ' + tag + ' + ') | (' + tag + ' +$)/g; '); Return This.replace (Reg, ");};/ /String intercept later added ... String.prototype.interceptString = function (len) {if (This.length > Len) {return this.substring (0, Len) + "    ...";    } else {return this; }}//converts a string into an array with the given character String.prototype.toArray = function (tag) {if (This.indexof (tag)! =-1) {return This.split (    TAG);        }else {if (This! = ") {return [this.tostring ()];        }else {return []; }}}//only leaves the number (0123456789) string.prototype.tonumber= function () {return this.replace (/\d/g, "");} Reserved Chinese string.prototype.tocn= FUnction () {var regEx =/[^\u4e00-\u9fa5\uf900-\ufa2d]/g;  Return This.replace (RegEx, ");    }//turns into intstring.prototype.toint= function () {var temp = This.replace (/\d/g, ""); Return IsNaN (parseint (temp))?  This.tostring (): parseint (temp); }//whether the string.prototype.startswith= function (tag) {return this.substring (0, tag.length) = = tag is prefaced with XX; Has the XX end string.prototype.endwith= function (tag) {return this.substring (this.length-tag.length) = = tag;} Stringbuffervar StringBuffer = function () {this._strs = new Array;}; StringBuffer.prototype.append = function (str) {this._strs.push (str);}; StringBuffer.prototype.toString = function () {return This._strs.join ("");};                                                             String.prototype.replaceAll = function (s1,s2) {return this.replace (new RegExp (S1, "GM"), S2);}
Array Tool class
/********************** Arry ***************///The index in the re-array according to the data Array.prototype.getIndex = function (obj) {for    (var i = 0; i < this.length; i++) {        if (obj = = This[i]) {            return i;        }    }    return-1;}  Move an element in an array array.prototype.remove= function (obj) {for    (var i = 0; i < this.length; i++) {        if (obj = = This[i]) {            This.splice (i, 1);            break;        }    }    return this;} Determines whether the element is in the array array.prototype.contains= function (obj) {for    (var i = 0; i < this.length; i++) {        if (obj = = Thi S[i]) {            return true;        }    }    return false;}                                                           
Browser-related actions
/********************** Browser related operations ***************///into full screen mode, judging various browsers, find the right way var launchfullscreen = function (Element) {if  (Element.requestfullscreen) {element.requestfullscreen ();  } else if (Element.mozrequestfullscreen) {element.mozrequestfullscreen ();  } else if (Element.webkitrequestfullscreen) {element.webkitrequestfullscreen ();  } else if (Element.msrequestfullscreen) {element.msrequestfullscreen (); } return true;  Exit full-screen mode var exitfullscreen = function () {if (Document.exitfullscreen) {document.exitfullscreen ();  } else if (Document.mozcancelfullscreen) {document.mozcancelfullscreen ();  } else if (Document.webkitexitfullscreen) {document.webkitexitfullscreen (); } return false;} Cookie operation var cookieutil={path: "/", Domain: ' demo.j2ee.com ', add:function (name,val) {$.cookie (name, Val    , {expires:7, Path:this.path, Domain:this.domain, secure:true}); }, Remove:function (name) {$.cookie (name, Null,{path:this.path, Domain:this.domain});    }, Get:function (name) {$.cookie (Name,{path:this.path, domain:this.domain}); }}//errorvar error={e_404:function () {alertmessage ("404", "changed page not found!")     "," warning "); }, E_500:function () {alertmessage ("500", "Server internal Error!     "," error "); }, E_403:function () {alertmessage ("403", "Insufficient permissions!")     "," warning ");                                                             }}
Source:http://www.htmleaf.com/ziliaoku/qianduanjiaocheng/201505311944.html

JS Tool Class

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.