JS implements HTML tag escape and reversal semantics

Source: Internet
Author: User

Today I encountered a problem with Ueditor:

I read the content from the database to edit, not some HTML tags, read from the database is no problem;

But I use ASP. NET MVC, the HTML tag is escaped when I assign the read content to the editing area through the Ueditor API method;

For example, < becomes &lt;

I follow the code to see the back end of the data no problem, back to the front page when the HTML tab is normal to see, but is directly escaped in JS, I still do not forget Ah, first in the back end with HTML encoding, Using the Httputility.htmlencode method to encode once, in the razor to use once again HtmlDecode, still can not solve the problem;

It can be seen that the browser is self-escaped, presumably because of various security reasons it, only the internet to find the code, through the JS to the HTML escape and reversal semantics, found the following code:

functionHTMLEncode (HTML) {vartemp = document.createelement ("div"); (Temp.textcontent!=NULL) ? (temp.textcontent = html): (Temp.innertext =html); varOutput =temp.innerhtml; Temp=NULL; returnoutput;}functionHtmlDecode (text) {vartemp = document.createelement ("div"); Temp.innerhtml=text; varOutput = Temp.innertext | |temp.textcontent; Temp=NULL; returnoutput;} 

This solves the problem! Thank you very much for this blogger code!

Address of the reference code: http://blog.600km.xyz/2015/12/15/js-encode-html-tags/

JS implements HTML tag escape and reversal semantics

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.