JS and jquery HTML content encoding, HTML escape

Source: Internet
Author: User

/** jquery html Encoding, decoding* principle is the use of jquery's own HTML () and text () functions can escape HTML characters * Virtual a div by assigning values and values to get the desired Html encoding or decoding */< Script src= "http://libs.baidu.com/jquery/1.9.0/jquery.js" ></script><script type= "Text/javascript" >//html encoding gets the HTML escape entity function HtmlEncode (value) {  return $ (' <div/> '). Text (value). html (); HTML decoding gets the HTML entity function HtmlDecode (value) {  return $ (' <div/> '). HTML (value). text ();} </script>


<script type= "Text/javascript" >//get HTML escape character function HtmlEncode (HTML) {  return document.createelement (' a ' ). AppendChild (          document.createtextnode (HTML)). parentnode.innerhtml;};/ /Get HTML function HtmlDecode (HTML) {  var a = document.createelement (' a '); a.innerhtml = html;  return a.textcontent;}; </script>

  Encoded    function Html_encode (str)    {        var s = "";        if (Str.length = = 0) return "";        s = str.replace (/&/g, ">");        s = S.replace (/</g, "<");        s = S.replace (/>/g, ">");        s = s.replace (//g, "");        s = s.replace (/\ '/g, "'");        s = s.replace (/\ "/g," "" ");        s = S.replace (/\n/g, "<br>");        return s;    }    Decode    function Html_decode (str)    {        var s = "";        if (Str.length = = 0) return "";        s = str.replace (/>/g, "&");        s = S.replace (/</g, "<");        s = S.replace (/>/g, ">");        s = s.replace (//g, "");        s = s.replace (/'/g, "\ '");        s = s.replace (/"/g," \ "");        s = S.replace (/<br>/g, "\ n");        return s;    }


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.