Home  >  Article  >  Web Front-end  >  JavaScript functions to HTML encode and decode strings_javascript tips

JavaScript functions to HTML encode and decode strings_javascript tips

WBOY
WBOYOriginal
2016-05-16 18:35:381001browse

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
}
Statement:
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