JavaScript HTMLEncode function (FF compatible edition) is mainly the reverse HTML code in the editor _javascript tips

Source: Internet
Author: User

Method One:

Use the browser internal converter to implement the conversion, the main point is to dynamically create a container tag elements, such as Div, the string to be converted is set to this element innertext (ie support) | | Textcontent (Firefox support), and then return the element of the innerHTML, that is, the HTML encoded conversion of the string, the display of the time can be reversed (in fact, when the display does not eliminate the conversion, directly assigned to the Div can be normal display).

<script type= "Text/javascript" > Function HTMLEncode (HTML) {var temp = document.createelement ("div"); (temp.textcontent!= null)? (temp.textcontent = html): (Temp.innertext = html); var output = temp.innerhtml; temp = null; return output; function HtmlDecode (text) {var temp = document.createelement ("div"); temp.innerhtml = text; var output = Temp.innertext | | Temp.textcontent; temp = null; return output; var html = "<br>dffdf<p>qqqqq</p>"; var encodehtml = HTMLEncode (HTML); Alert ("Method one:" +encodehtml); var decodehtml = HtmlDecode (encodehtml); Alert ("Method one:" +decodehtml); </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

The second method: regular replacement
By converting the regular expression to HTML encoding <> and spaces, because this method is not built into the system, it is very easy to have some special tags that are not replaced and inefficient
<textarea id="runcode20644"><script type= "Text/javascript" > Function HTMLEncode2 (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", "&quot;"); return s; function HTMLDecode2 (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 (/&quot;/g, "\"); return s; var html = "<br>cccccaaaaa"; var encodehtml = HTMLEncode2 (HTML); Alert ("Method two:" +encodehtml); var decodehtml = HTMLDecode2 (encodehtml); Alert ("Method two:" +decodehtml); </script></textarea>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

We can run the test first, I also found that the first method is more useful ah, really good, we must remember.
In addition, some editors use some HTMLEncode functions, then you need to add, but the need to remind that the code must be tested AH, cloud Habitat Community jb51.net Webmaster Release This message when the test is really troublesome Ah, modified a number of times
Copy Code code as follows:

function HTMLEncode (text) {
Text = Text.replace (/&/g, "&");
Text = text.replace (/"/g," "");
Text = Text.replace (/</g, "<");
Text = Text.replace (/>/g, ">");
Text = Text.replace (/\/g, "");
Text = Text.replace (/\n/g, "<br>");
Text = Text.replace (/\t/g, "");
return text;
}

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.