60

I have a javascript function which passes as a query string value another query string.

In other words, I want the query string to be:

http://www.somesite.com/?somequery=%3fkey%3dvalue1%2520%26%2520key2%3value3

However, if I redirect like this:

var url = 'http://www.somesite.com/?somequery=';
url += escape('?key=value1&key2=value2');
window.location = url;

it ends up as http://www.somesite.com?somequery=?key1=value1&key2=value2 in firefox and IE7 which means that I can't parse the query string correctly.

I also tried using encodeURIComponent which didn't work either.

Is there another function or a hack to force the redirect to keep the somequery value escaped??

Ilia Anastassov
  • 368
  • 8
  • 16
rmw
  • 1,253
  • 1
  • 12
  • 20

4 Answers4

111

encodeURIComponent will work. (You may or may not want the leading ‘?’, depending on what the script is expecting.)

var c= 'd e'
var query= '?a=b&c='+encodeURIComponent(c);
var uri= 'http://www.example.com/script?query='+encodeURIComponent(query);
window.location= uri;

Takes me to:

http://www.example.com/script?query=%3Fa%3Db%26c%3Dd%2520e

When you hover over that it may appear once-decoded in the browser's status bar, but you will end up in the right place.

escape/unescape() is the wrong thing for encoding query parameters, it gets Unicode characters and pluses wrong. There is almost never a case where escape() is what you really need.

bobince
  • 528,062
  • 107
  • 651
  • 834
  • Yes, I'm pretty sure escape is deprecated. Good call. – Eric Wendelin Feb 27 '09 at 23:32
  • This works - although when parsing the query string I have to decode it twice now. It seems that when it is encoded once, it will be decoded by the browser but when it's encoded twice it won't be decoded at all. strange. – rmw Mar 02 '09 at 15:49
2

Native escape method does that. but also you can create a custom encoder like:

function encodeUriSegment(val) {
  return encodeUriQuery(val, true).
             replace(/%26/gi, '&').
             replace(/%3D/gi, '=').
             replace(/%2B/gi, '+');
}

this will replace keys used in query strings. further more you can apply it to any other custom encodings by adding needed key-values pairs.

Reyraa
  • 4,174
  • 2
  • 28
  • 54
0
function downloadFile(){
      var filePath = "C:/Users/HP/Desktop/Project Folder/DemoProject/";

      var fileInfo = "Error_Issue Minor Cosmetic & Major Fatal Issues (Demo_Project) (2017)_GeneratedOn_12_21_2017 21924 AM.xlsx";
      if((filePath != undefined && filePath.length > 0) && (fileName != undefined && fileName.length > 0)){
        var downloadUrl = "../download?fileName="+encodeURIComponent(fileName)+"&filePath="+encodeURIComponent(filePath);
        $window.location = downloadUrl;
      }else{
        alert("Please define a fileName for downloading...");
      }
}
Circle Hsiao
  • 1,497
  • 4
  • 22
  • 37
BRS13
  • 1
  • 2
-3
javascript:alert(escape('?key=value1&key2=value2'));

Works fine for me?

Gary Green
  • 22,045
  • 6
  • 49
  • 75