jQuery 在 POST 之前编码 HTML

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4899005/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 18:09:40  来源:igfitidea点击:

Encode HTML before POST

htmljqueryposthtml-encode

提问by oshirowanen

I have the following script, which encodes some of the value it receives propertly, but it does not seem to encode double quotes.

我有以下脚本,它对它正确接收的一些值进行编码,但它似乎没有对双引号进行编码。

How do I encode the full value properly before posting?

在发布之前如何正确编码完整值?

function htmlEncode(value){ 
    return $('<div/>').text(value).html(); 
} 

The above script give me this:

上面的脚本给了我这个:

&lt;p&gt;Test&amp;nbsp; &lt;span style="color: #ffffff"&gt;&lt;strong&gt;&lt;span style="background-color: #ff0000"&gt;1+1+1=3&lt;/span&gt;&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;

I need it to give me this:

我需要它给我这个:

&lt;p&gt;Test&amp;nbsp; &lt;span style=&quot;color: #ffffff&quot;&gt;&lt;strong&gt;&lt;span style=&quot;background-color: #ff0000&quot;&gt;1+1+1=3&lt;/span&gt;&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;

EDIT: Followup question: Encoded HTML in database back to page

编辑:后续问题: 在数据库中编码的 HTML 返回页面

回答by George Stocker

You shouldn't try to encode things with JavaScript.

你不应该尝试用 JavaScript 编码东西。

You should encode it serverside.

您应该在服务器端对其进行编码。

Anything that can be done with JavaScript can be undone.

任何可以用 JavaScript 完成的事情都可以撤消。

It is valid to encode it in JavaScript if you also check that it was encoded on the server, but keep in mind: JavaScript can be disabled.

如果您还检查它是否在服务器上编码,则用 JavaScript 对其进行编码是有效的,但请记住:可以禁用 JavaScript。

回答by Labu

What George says is true. But, if you have to encode strings client-side, I'd suggest you use JavaScript's encodeURIComponent().

什么乔治说的是真的。但是,如果您必须在客户端对字符串进行编码,我建议您使用 JavaScript 的encodeURIComponent()

回答by MMK

I had a similar problem. I simply used the replace method in javascript. Here's a nice article to read: http://www.w3schools.com/jsref/jsref_replace.asp

我有一个类似的问题。我只是在javascript中使用了replace方法。这是一篇不错的文章:http: //www.w3schools.com/jsref/jsref_replace.asp

Basically what the replace method does is it swaps or replaces the character it founds with what you indicate as replacement character(s).

基本上,replace 方法的作用是用您指示的替换字符交换或替换它找到的字符。

So this:

所以这:

var str=' " That " ';
str = str.replace(/"/g,'&quot;');

Once you log this into the console of your browser, you will get something like

将其登录到浏览器的控制台后,您将获得类似的信息

&quot; That &quot;

And this:

和这个:

var str=' " That " ';
str = str.replace(/"/g,'blahblahblah');

Once you log this into the console of your browser, you will get something like

将其登录到浏览器的控制台后,您将获得类似的信息

blahblahblah That blahblahblah

回答by andres descalzo

You can use this module in js, without requiring jQuery:

你可以在 js 中使用这个模块,而不需要 jQuery:

htmlencode

html编码

回答by Distdev

You can re-use functions from php.js project - htmlentitiesand get_html_translation_table

您可以重用 php.js 项目中的函数 - htmlentitiesget_html_translation_table

回答by Sohel Pathan

Use escape(str) at client side

在客户端使用escape(str)

and

HttpUtility.UrlDecode(str, System.Text.Encoding.Default); at server side

HttpUtility.UrlDecode(str, System.Text.Encoding.Default); 在服务器端

it worked for me.

它对我有用。