0

I have a string for example, that may contain special characters (+,=,&, 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!

2 Answers2

2

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!"
Arnaud Leymet
  • 5,995
  • 4
  • 35
  • 51
1

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.

Community
  • 1
  • 1
Arnaud Leymet
  • 5,995
  • 4
  • 35
  • 51