Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Percent encoding javascript

Tags:

Is there a javascript function that takes a string and converts it into another string that is percent-encoded? That way something like "This Guy" turns into "This%20Guy".

Thanks

like image 378
locoboy Avatar asked Feb 06 '11 05:02

locoboy


People also ask

What is encodeURI in javascript?

The encodeURI() function encodes a URI by replacing each instance of certain characters by one, two, three, or four escape sequences representing the UTF-8 encoding of the character (will only be four escape sequences for characters composed of two "surrogate" characters).


1 Answers

encodeURI, encodeURIComponent or escape will work the same way for your string, but they differ in details.

encodeURI is just for escaping URLs
encodeURIComponent also escapes = and &
escape works differently with non-ASCII unicode symbols

encodeURI("Ω") === encodeURIComponent("Ω") === "%CE%A9" escape("Ω") === "%u03A9" 

if you need to send a string as part of request, use encodeURIComponent

like image 165
kirilloid Avatar answered Oct 19 '22 11:10

kirilloid