The most practical and comprehensive tool class method at the front end

Source: Internet
Author: User
Tags convert string to json string to json

1. Why do you want to do this thing?
I personally recently always feel that writing the foreground code is a lot of plagiarism, and each copy of the place is from a different project, the time to knock code is often less than the time to find code;
Small program more aggravating the front-end JS in the programming of the component, learn, good summary is very necessary;
Sophisticated to eliminate the redundancy of the Code;
Sharing is a thing, the things of the Lele;

2, how to do this thing
From a large number of programs to select the most common JS method summary;
Find common methods on the Internet, collect;
Divided into large categories to summarize;

3. Major categories

Query Class (Ajax request, form request)

Check class (regular expression, date, check weight)

Data Conversion processing class (date class, String Class)

Cryptographic decryption Class (base64)


4. Start summarizing now

I. Query class (AJAX request, form request)

Ajax requests:

Requestjsonrs:function(URL, param, async, callback) {if(!param) {param= {}        }        varJsonobj =NULL; $.ajax ({type:"POST", DataType:"HTML", Url:url, Data:param, async: (Async? Async:false), Success:function(data) {Try{jsonobj= eval ("(" + Data + ")")                } Catch(e) {jsonobj=NULL                }                if(callback) {callback (Jsonobj)}}, Error:function(XMLHttpRequest, Textstatus, Errorthrown) {jsonobj={rtmsg:"Ajax Request Error"}}, timeout:30000        }); returnJsonobj}

Second, check class (regular expression, date, check weight)

// Check your phone number function  var str=document.getElementById (STR). value; Regularexpif  (Regularexp.test (STR)) {  returntrue   else  {  alert ("phone number is not formatted correctly!) Should be a 11-bit length number! ");   return false ; }}

Third, Data conversion processing class (date class, String Class)

Convert string to JSON format:

function (str) {        var json = eval ("(" + str + ")");         return JSON    }

Form converted to JSON format:

Formtojson:function(c, b) {varA = {}; if(!b) {$ (c). Find ("Input[name][disabled!=disabled]"). each (function(d, E) {a[$ (E). attr ("Name")] =$ (E). Val ()}); $ (c). Find ("Input[name][type=hidden][disabled!=disabled]"). each (function(d, E) {a[$ (E). attr ("Name")] =$ (E). Val ()}); $ (c). Find ("Textarea[name][disabled!=disabled]"). each (function(d, E) {a[$ (E). attr ("Name")] =$ (E). Val ()}); $ (c). Find ("Select[name][disabled!=disabled]"). each (function(d, E) {a[$ (E). attr ("Name")] =$ (E). Val ()}); $ (c). Find ("Input[name][type=checkbox][disabled!=disabled]"). each (function(d, e) {if($ (E). attr ("Checked") {a[$ (e). attr ("Name")] = 1                        } Else{a[$ (e). attr ("Name")] = 0                        }                    }); $ (c). Find ("Input[name][type=radio][disabled!=disabled]:checked"). each (function(d, E) {a[$ (E). attr ("Name")] =$ (E). Val ()}); $ (c). Find ("Input[name][type=password][disabled!=disabled]"). each (function(d, E) {a[$ (E). attr ("Name")] =$ (E). Val ()}) } Else{$ (c). Find ("Input[name]"). each (function(d, E) {a[$ (E). attr ("Name")] =$ (E). Val ()}); $ (c). Find ("Input[name][type=hidden]"). each (function(d, E) {a[$ (E). attr ("Name")] =$ (E). Val ()}); $ (c). Find ("Textarea[name]"). each (function(d, E) {a[$ (E). attr ("Name")] =$ (E). Val ()}); $ (c). Find ("Select[name]"). each (function(d, E) {a[$ (E). attr ("Name")] =$ (E). Val ()}); $ (c). Find ("Input[name][type=checkbox]"). each (function(d, e) {if($ (E). attr ("Checked") {a[$ (e). attr ("Name")] = 1                } Else{a[$ (e). attr ("Name")] = 0                }            }); $ (c). Find ("Input[name][type=radio]:checked"). each (function(d, E) {a[$ (E). attr ("Name")] =$ (E). Val ()}); $ (c). Find ("Input[name][type=password]"). each (function(d, E) {a[$ (E). attr ("Name")] =$ (E). Val ()}) }        returnA}

Iv. encryption and Decryption Class (base64)

Base64 bit Encryption

Define several concepts:
The charCodeAt () method returns the Unicode encoding of the character at the specified position. This return value is an integer between 0-65535.

fromCharCode () can accept a specified Unicode value and then return a string.

Encode64:function(d) {d=Strunicode2ansi (d); varb = "Abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789+/="; varA = ""; varL, j, g = ""; varK, H, f, E = ""; varc = 0;  Do{L= D.charcodeat (c + +); J= D.charcodeat (c + +); G= D.charcodeat (c + +); K= L >> 2; H= ((L & 3) << 4) | (J >> 4); F= ((J &) << 2) | (G >> 6); E= G & 63; if(IsNaN (j)) {f= e = 64            } Else {                if(IsNaN (g)) {e= 64}} A= A + B.charat (k) + B.charat (h) + B.charat (f) +B.charat (e); L= j = g = ""; K= h = f = e = ""        }  while(C <d.length); returnA}functionStrunicode2ansi (g) {varc =g.length; varA = "";  for(vard = 0; D < C; d++) {            varf =G.charcodeat (d); if(F < 0) {f+ = 65536            }            if(F > 127) {f=Unicodetoansi (f)}if(F > 255) {                varb = f & 65280; b= B >> 8; varE = f & 255; A+ = String.fromCharCode (b) +String.fromCharCode (e)}Else{a+=String.fromCharCode (f)}} returnA}

Force uppercase conversion to lowercase

functionif(event.keycode>=65 && event.keycode<=90)  Event.keycode =event.keycode+32;}

Rounding a decimal
Decimalnum: Keep several digits after the decimal point

function roundfloat (str, decimalnum) {    var sourcevalue=if  (IsNaN (sourcevalue)) {     alert ("sourcevalue illegal character");     document.getElementById (str). Value= "";   return  var multiplyvalue=math.pow (Ten, parseint (Decimalnum)); document.getElementById (str). Valuereturn (Math.Round (multiplyvalue*sourcevalue))/multiplyvalue;}

The most practical and comprehensive tool class method at the front end

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.