The javascript htmlencode function (FF compatible version) is mainly used to reverse the HTML code in the editor.

Source: Internet
Author: User

Method 1:

Use a browser internal converter to implement conversion. The essentials are to dynamically create a container label element, such as Div, and set the string to be converted to the innertext of this element (ie supported) | textcontent (supported by Firefox), and then returns the innerhtml of this element to obtain the HTML-encoded string, which can be displayed in turn (in fact, the conversion is not needed when the display is complete, directly assign a value to the DIV to display it normally ).

<textarea id="runcode93589"></textarea>
[Ctrl + A select all Note: If you need to introduce external JS, You need to refresh it to execute]

Method 2: Regular Expression replacement
The regular expression is used to convert <> and space characters into HTML encoding. Because this method is not built-in to the system, it is easy to see that some special labels are not replaced, and the efficiency is low.
[Ctrl + A select all Note: If you need to introduce external JS, You need to refresh it to execute]

You can run the test first. I also found that the first method is quite easy to use. It's really good. You must remember it.
In addition, some htmlencode functions are used by some editors. You can add them as needed. However, please note that,CodeBe sure to test it. It is really troublesome to test it when the jb51.net webmaster posts this information. It has been modified many times.Copy codeThe Code is 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;
}

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.