Quickest way to search and replace in a query string?

L

laredotornado

Hi,

Given the search or query string from a URL ...

var searchStr = location.search;

How would i write a function, replace(paramName, paramVal), to replace
the value in the query string belonging to the param name,
"paramName", or, if there is no "paramName" given in the query string,
add it to the existing query string?

Thanks for your help, - Dave
 
L

Luuk

laredotornado schreef:
Hi,

Given the search or query string from a URL ...

var searchStr = location.search;

How would i write a function, replace(paramName, paramVal), to replace
the value in the query string belonging to the param name,
"paramName", or, if there is no "paramName" given in the query string,
add it to the existing query string?

Thanks for your help, - Dave

http://lmgtfy.com/?q=how+to+replace+param+in+url

1st link looks good....
http://www.webmasterworld.com/javascript/3986111.htm
 
T

Thomas 'PointedEars' Lahn

laredotornado said:
Given the search or query string from a URL ...

var searchStr = location.search;

How would i write a function, replace(paramName, paramVal), to replace
the value in the query string belonging to the param name,
"paramName", or, if there is no "paramName" given in the query string,
add it to the existing query string?

You could use a keyboard.

<http://www.catb.org/~esr/faqs/smart-questions.html>


PointedEars
 
C

Csaba Gabor

var searchStr = location.search;

How would i write a function, replace(paramName, paramVal), to replace
the value in the query string belonging to the param name,
"paramName", or, if there is no "paramName" given in the query string,
add it to the existing query string?

Untested, but possibly something like:
first protect paramName and paramVal with one
of the encoding functions:
escape(), encodeURI(), or encodeURIComponent()
and then

var re = new RegExp('\\?' + paramName + '=[^?]*');
var newItem = '?' + paramName + '=' + paramVal;
var newSearchStr = (('?'+searchStr)
.replace(re, "") + newItem).substr(1)

Csaba Gabor from Vienna
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top