user3835327
user3835327

Reputation: 1204

url encode & decode parameter and read parameter

i success encode & decode the url parameter but how can i get the parameter after decode?? the reason i encode all query strings into a parameter just to prevent user change the parameter on the address bar.

For example

Page A

  function fnlink()
    {
        param1 = encodeURIComponent("INSCODE=91&NAME=LEE&EMAIL=abc");
    url = "/home/test/test2.jsp?"+param1;

    location.href= url; 
    }

Page B

url : http://localhost:9080/home/test/test2.jsp?INSCODE%3D91%26NAME%3DLEE%26EMAIL%3Dabc

Upvotes: 2

Views: 2196

Answers (1)

freedev
freedev

Reputation: 30217

You should not encode the entire parameters string "INSCODE=91&NAME=LEE&EMAIL=abc" with encodeURIComponent.

Each parameter should be encoded separately. Use a Javascript function like this to add your parameters at query string:

/**
* Add a URL parameter 
* @param {url}   string  url 
* @param {param} string  the key to set
* @param {value} string  value 
*/
var addParam = function(url, param, value) {
   param = encodeURIComponent(param);
   var a = document.createElement('a');
   param += (value ? "=" + encodeURIComponent(value) : ""); 
   a.href = url;
   a.search += (a.search ? "&" : "") + param;
   return a.href;
}

Upvotes: 2

Related Questions