convert UTF 8 string to URL (ajax)
I have a string for example, that may contain special charac开发者_高级运维ters (+
,=
,&
, etc...):
"Írja ide kérdését, majd üssön entert!"
and I would like to convert it to URL accepteble string for XHR like (because IE does not do it automatically):
"%C3%8Drja%20ide%20k%C3%A9rd%C3%A9s%C3%A9t,%20majd%20%C3%BCss%C3%B6n%20entert!"
Is there any javascript function for this?
Thank you!
You should use the encodeURI
function:
encodeURI("Írja ide kérdését, majd üssön entert!");
// => "%C3%8Drja%20ide%20k%C3%A9rd%C3%A9s%C3%A9t,%20majd%20%C3%BCss%C3%B6n%20entert!"
You have added a +
sign.
To encode this plus sign too, use the encodeURIComponent
function:
encodeURIComponent("+Írja ide kérdését, majd üssön entert!");
// => "%2B%C3%8Drja%20ide%20k%C3%A9rd%C3%A9s%C3%A9t%2C%20majd%20%C3%BCss%C3%B6n%20entert!"
Check this thread for more informations about the differences between escape
, encodeURI
and encodeURIComponent
functions.
精彩评论