JS Common Code Snippets

Source: Internet
Author: User
Tags html encode

Some of the public JS that will be used recently will continue to be supplemented.

String extensions (function () {var fn = String.prototype;        Fn.format = function () {var args = arguments; Return This.replace (/\{(\d+) \}/g, function (M, i) {return args[i] | |        "";    }); }})();

String formatting "Name:{0},value:{1}". Format ("P0", "P1"): "Name:p0,value:p1"

JSON transform

(function (WD) {    var transform = function (oldobj, prefix, Subobjorvalue, includefunc) {        if (Subobjorvalue = = null) {            Oldobj[prefix] = null;      &N Bsp } else {            if (subobjorvalue instanceof Array) {                for (var i = 0; i < subobjorvalue.length; i++) {              & nbsp     var Newpex = prefix + "[" + i + "]";                    TR Ansform (Oldobj, Newpex, Subobjorvalue[i], Includefunc);               }  &NB Sp        } else if (typeof subobjorvalue = = = "Object") {            &NBSP ;   for (var name in Subobjorvalue) {                    if (subobjor Value.hAsownproperty (name)) {                        var Newobjpex = "";                        if (prefix.length = = = 0) {  &N Bsp                         NEWOBJPEX = name + "";    &N Bsp                  } else {              &N Bsp             Newobjpex = prefix + "." + name;                       }                        T Ransform (Oldobj, Newobjpex, Subobjorvalue[name], Includefunc);                &NB Sp  }               }           } else {                if (includefunc) {                &NBSP ;   Oldobj[prefix] = subobjorvalue;               } else {                    if (typeof subobjorvalue!== "function") {        &N Bsp               Oldobj[prefix] = subobjorvalue;                   }               }          & nbsp }       }   }    WD. Json.__transformtosimple = function (jsonobj, prefix, includefunc) {        if (!jsonobj) {  &nbs P         return null;       }        var obj = {};  &nbsp ;     transform (obj, prefix | | "", Jsonobj, Includefunc);nbsp       return obj;   };}) (window); The JSON in JS is converted to simple JSON, multiple levels of JSON, converted to only one level of JSON, easy to simulate form submission.

{"A": "V-1", "B": {"b-1": "V-b1", "b-2": "V-b2"}, "C": ["V-c1", "V-C2"]}

Converted to

{"A": "V-1", "b.b-1": "V-b1", "b.b-2": "V-b2", "c[0]": "V-c1", "c[1]": "V-C2"}

HTML encode or decode (function (wd) {    Wd.__htmlencode = function (str) {        return ("" + str). Replace (/&/g, "&amp;")            . Replace (/>/g, "&gt;")            . Replace (/</g, "&lt;")            . Replace (//g, "&nbsp;")            . replace (/\ '/g, "'")            . replace (/\ "/g," "& Amp;quot; ");    };    Wd.__htmldecode = function (str) {        return ("" + str). Replace (/&am P;amp;/g, "&")            . Replace (/&gt;/g, ">")         &NBSP ;   Replace (/&lt;/g, "<")            . Replace (/&nbsp;/g, "")     &N Bsp       replace (/'/g, "\ '")            . Replace (/&quot;/g, "\"); &nbsP  };}) (window);


HTML encoding, the special symbols in the HTML code, to prevent interference damage to normal HTML.

JS Common Code Snippets

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.