This has got to be an easy one, but I'm just not getting it.
The following function (below) returns name=value pairs from URL queries
(GET). All is working as expected, but I am trying to figure out how to
remove the + (plus signs) from the values that contain spaces.
I'm trying to use the String Object's replace() method to accomplish this.
This is what I've tried so far along with the results:
value.replace('+', ' ') = first and only first occurrence of + is
replaced
value.replace('+'/g, ' ') = error: "g is not defined"
value.replace(/ /g, ' ') = doesn't replace any + with a space
value.replace(' '/g, ' ') = error: "g is not defined"
value.replace(/' '/g, ' ') = doesn't replace any + with a space
function getArgs() {
var args = new Object();
var query = location.search.substring(1); // Get query string
var pairs = query.split("&"); // Break at ampersand
for(var i = 0; i < pairs.length; i++) {
var pos = pairs[i].indexOf('='); // Look for "name=value"
if (pos == -1) continue; // If not found, skip
var argname = pairs[i].substring(0,pos); // Extract the name
var value = pairs[i].substring(pos+1); // Extract the value
value = decodeURIComponent(value); // Decode it, if needed
value = value.replace(/+/g, ' '); // replace plus with space
args[argname] = value; // Store as a property
}
return args;
}
I'm running out of ideas. ?? Can anyone guide me in the right direction?
Thanks all...
--
------------------------------------
Gene Kelley
Villa Ridge, Missouri, USA