Home > Web Front-end > JS Tutorial > body text

JavaScript functions to HTML encode and decode strings_javascript tips

WBOY
Release: 2016-05-16 18:35:38
Original
998 people have browsed it

Encoding function:

Copy code The code is as follows:

function HtmlEncode(str) {
var t = document.createElement("div");
t.textContent ? t.textContent = str : t.innerText = str;
return t.innerHTML;
}

Decoding function:
Copy code The code is as follows:

function HtmlDecode(str) {
var t = document.createElement("div");
t.innerHTML = str;
return t.innerText || t.textContent
}
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