Forum Moderators: open
I am trying to figure out a way to replace query parameters using JS and having a hard time figuring this out. Any help will be greatly appreciated.
In code, get_param identifies st_price but it is unable to replace the string.
_uri = '/default.aspx?&search[st_date]=09/09/2009&search[en_date]=09/10/2009&search[st_rate]=500&search[en_rate]=900';
_new_url = _uri.replace(get_param('search[st_price]'),'search[st_price]'+ui.values[0])
function get_param( name )
{
name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regexS = "[\\?&]"+name+"=([^&#]*)";
var regex = new RegExp( regexS );
var results = regex.exec( window.location.href );
if( results == null ) return "";
else {
_ret = name+'='+results[1];
return _ret;
}
function replace_param(theURL, paramName, newValue) {
var uri_array = theURL.split('?'); // break up URL/query
var params_array = uri_array[1].split('&'); // break up the query
var items_array = new Array;
for (i=0; i<params_array.length; i++) {
items_array = params_array[i].split('='); // split name/value pairs
if (items_array[0] == paramName) {
params_array[i] = items_array[0] + '=' + newValue;
} // end if
} // end for i
return uri_array[0] + '?' + params_array.join('&');
} // end function
Caution: Untested code written on the fly.