HTML encoding and decoding

Source: Internet
Author: User

 var htmlutil = {
/*1. Implements HTML transcoding */
Htmlencode:function (HTML) {
//1 with a browser internal translator. First, dynamically create a container label element, such as D IV
var temp = document.createelement ("div");
2. Then set the string to be converted to this element of innertext (ie support) or textcontent (Firefox, Google support)
(temp.textcontent! = undefined)? (temp.textcontent = html): (Temp.innertext = html);
//3. Finally returns the InnerHTML of this element, which is a string that has been converted by HTML encoding
var output = temp.innerhtml;
temp = null;
return output;
},
/*2. Implement HTML decoding with the browser internal translator */
Htmldecode:function (text) {
//1. First, dynamically create a container tag element, such as Div
var temp = document.createelement ("div");
//2. Then set the string to be converted to this element's InnerHTML (ie, Firefox, Google support)
temp.innerhtml = text;
3. Finally return this element of the innertext (ie support) or textcontent (Firefox, Google support), that is, the HTML decoded string.
var output = Temp.innertext | | temp.textcontent;
temp = null;
return output;
}
};

Summary: encoded by innerHTML Way, by innertext | | Textcontent mode decoding

HTML encoding and decoding

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.