2

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

6
  • 2
    Possible duplicate of Parse a URI String into Name-Value Collection Commented Mar 6, 2017 at 14:03
  • stackoverflow.com/questions/13592236/… shows you how to use URL.getQuery to extract the query part and then use String.split to get the individual parts into a map. Then use the map's methods to check & get values for specific parameters. Commented Mar 6, 2017 at 14:05
  • 1
    The method getParameter of request object returns the parameter you're looking already decoded. Not clear what's your problem. Commented Mar 6, 2017 at 14:05
  • @freedev getParameter is a method that the OP wished existed. Commented Mar 6, 2017 at 14:07
  • Just so you are aware.. You are appending the QueryString parameters wrong. It should be: request.getRequestURL() + "?" + request.getQueryString().. Note the question mark. Commented Mar 6, 2017 at 14:17

1 Answer 1

2

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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

i tried this method, my parameter did not encode .. when redirect my parameter still show exact parameter ..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.