开发者

In JavaScript or jQuery, how do you convert a single string with three words ("abc def hij") into the url encoded format: "abc+def+hij"?

I need to encode some strings to add to a URL.

My str开发者_运维知识库ing contains multiple words: "abc def hij" (i.e. not three separate strings but a single string with 3 words in it).

In JavaScript or jQuery, how do you convert a string like that ("abc def hij") into the encoded format: "abc+def+hij"?


encodeURIComponent converts the string into percent encoding.

Additionally,

For application/x-www-form-urlencoded (POST), per http://www.w3.org/TR/html401/interac...m-content-type, spaces are to be replaced by '+', so one may wish to follow a encodeURIComponent replacement with an additional replacement of "%20" with "+".

so a simple

string.replace(/%20/g, "+");

should do.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜