Home > Web Front-end > JS Tutorial > Complete example of javascript HTMLEncode HTMLDecode (compatible with IE and Firefox)_javascript skills

Complete example of javascript HTMLEncode HTMLDecode (compatible with IE and Firefox)_javascript skills

WBOY
Release: 2016-05-16 18:51:55
Original
1376 people have browsed it

Method 1:
Use the browser's internal converter to achieve conversion. The method is to dynamically create a container tag element, such as DIV, and set the string to be converted to the innerText of this element (supported by IE)||textContent (supported by Firefox) , and then return the innerHTML of this element, that is, get the string converted by HTML encoding, which can be reversed when displayed (actually, there is no need to go through conversion when displaying, and it can be displayed normally by assigning the value directly to the div).

Copy code The code is as follows:



Method 2:
By changing the regular expression to < ;> and space characters are converted into HTML encoding. Since this method is not built-in in the system, it is easy to have some special tags not replaced, and it is inefficient
Copy Code The code is as follows:


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template