[英]Javascript encode HTML entities on server
在我的服務器應用程序中(在Parse Cloud Code上),我想保存一些字符串數據。 這里有HTML實體,我想編碼。
所以我找到了一個使用Javascript的解決方案:
var txt = document.createElement("textarea");
txt.innerHTML = html;
return txt.value;
此代碼完美地適用於存在document
html頁面。 但是服務器上沒有這樣的變量。
我如何申報文件變量? 或者您可能知道另一種編碼HTML實體的解決方案。
您可以在Node上使用html-entities ,安裝如下:
npm install html-entities
然后你得到了entities.encode(..)
和entities.decode(..)
函數:
var Entities = require('html-entities').XmlEntities;
entities = new Entities();
console.log(entities.encode('<>"\'&©®')); // <>"'&©®
在gihub repo上有更多使用部分的例子。
function encode(r){ return r.replace(/[\\x26\\x0A\\<>'"]/g,function(r){return"&#"+r.charCodeAt(0)+";"}) } test.value=encode('How to encode\\nonly html tags &<>\\'" nice & fast!'); /************* * \\x26 is &ersand (it has to be first), * \\x0A is newline, *************/
<textarea id=test rows=11 cols=55>www.WHAK.com</textarea>
自從我問這個問題以來,我學習了JavaScript和AJAX。 所以,我的建議是使用AJAX和JSON進行瀏覽器和服務器端之間的通信。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.