Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Encode special characters to pass in url and read by javascript [duplicate]

I need to pass some parameters in the url and they can have special characters like ", spanish Ñ or ñ, : spaces and accents.

What is the propper way to encode them before adding to the url or in case I got in the html like that, read them?

I tried this:

arrayData[i] = pair[1].replace('+', " ").replace('%22', "\"");

But just get working with + or spaces, not both at the same time or in 2 lines:

    arrayData[i] = pair[1].replace('+', " ");
    arrayData[i] = pair[i].replace('%22', "\"");
like image 807
Biribu Avatar asked Sep 12 '14 11:09

Biribu


2 Answers

You can try encodeUri Built-in function, for example

encodeURI('coño funcionó!')
like image 73
Manjar Avatar answered Oct 23 '22 23:10

Manjar


Previous answer is correct. JavaScript has built in functions for fulfilling this kind of tasks.

You can try to investigate these functions in w3schools.com. Here are the links with basic information and live "Try it out" feature:

  • encodeURI - takes string with your characters and encodes it into plausible for url style ( encoding spaces and non ANSII chars )
  • decodeURI - takes encoded string and decodes it to initial state
like image 38
zmii Avatar answered Oct 23 '22 23:10

zmii