Thursday, May 23, 2024
 Popular · Latest · Hot · Upcoming
31
rated 0 times [  36] [ 5]  / answers: 1 / hits: 21714  / 13 Years ago, sun, february 5, 2012, 12:00:00
 #&q=car&category=Car%20Audio%2CAccessories&brand=


I borrowed a this function from a previous question asked on SO:



function insertParam(key, value)
{
key = escape(key); value = escape(value);

var kvp = document.location.hash.substr(1).split('&');

var i=kvp.length; var x; while(i--)
{
x = kvp[i].split('=');

if (x[0]==key)
{
x[1] = value;
kvp[i] = x.join('=');
break;
}
}

if(i<0) {kvp[kvp.length] = [key,value].join('=');}

//this will reload the page, it's likely better to store this until finished

document.location.hash = kvp.join('&');
}


I use it like this:



    insertParam(category,xy);
insertParam(brand,zy);


My problem is it is decoding comma's to %2C. I know I can handle the characters on the server side, but how can I make it look pretty with javascript? By pretty I mean replace %2c with a comma.


More From » javascript

 Answers
13

decodeURIComponent(foo) is the thing you are looking for.



Edit: Misread your question.



Use replace(/&/g, %26).replace(/=/g, %3D) instead of escape on key and value to do this.



None of the 3 functions encodeURI, encodeURIComponent or encode work for this task, because they either encode commas or don't encode &=.


[#87639] Thursday, February 2, 2012, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jericho

Total Points: 204
Total Questions: 98
Total Answers: 108

Location: Thailand
Member since Thu, Apr 22, 2021
3 Years ago
;