javascript - When are you supposed to use escape instead of encodeURI / encodeURIComponent? -


when encoding query string sent web server - when use escape() , when use encodeuri() or encodeuricomponent():

use escape:

escape("% +&="); 

or

use encodeuri() / encodeuricomponent()

encodeuri("http://www.google.com?var1=value1&var2=value2");  encodeuricomponent("var1=value1&var2=value2"); 

escape()

special characters encoded exception of: @*_+-./

the hexadecimal form characters, code unit value 0xff or less, two-digit escape sequence: %xx. characters greater code unit, four-digit format %uxxxx used.

https://developer.mozilla.org/en-us/docs/web/javascript/reference/global_objects/escape

encodeuri()

use encodeuri when want working url. make call:

encodeuri("http://www.example.org/a file spaces.html") 

to get:

http://www.example.org/a%20file%20with%20spaces.html 

don't call encodeuricomponent since destroy url , return

http%3a%2f%2fwww.example.org%2fa%20file%20with%20spaces.html 

encodeuricomponent()

use encodeuricomponent when want encode value of url parameter.

var p1 = encodeuricomponent("http://example.org/?a=12&b=55") 

then may create url need:

var url = "http://example.net/?param1=" + p1 + "&param2=99"; 

and complete url:

http://example.net/?param1=http%3a%2f%2fexample.org%2f%ffa%3d12%26b%3d55&param2=99

note encodeuricomponent not escape ' character. common bug use create html attributes such href='myurl', suffer injection bug. if constructing html strings, either use " instead of ' attribute quotes, or add layer of encoding (' can encoded %27).

for more information on type of encoding can check: http://en.wikipedia.org/wiki/percent-encoding


Comments

Popular posts from this blog

c++ - How do I get a multi line tooltip in MFC -

asp.net - In javascript how to find the height and width -

c# - DataTable to EnumerableRowCollection -