Gloria
Gloria

Reputation: 1325

Clean URL by removing one paramater and its value

Let's say I have the following URL addresses:

www.mydomain.com/default.aspx?param1=value1&param2=value2&param3=value3
OR
www.mydomain.com/default.aspx?param2=value2&param1=value1
OR
www.mydomain.com/default.aspx?param3=value3&param1=value1&param2=value2

How can I remove only the part "param1=value1" from those URL addresses with jQuery or Javascript?

Upvotes: 0

Views: 70

Answers (2)

Thomas Baumann
Thomas Baumann

Reputation: 111

try

var url = "www.mydomain.com/default.aspx?param3=value3&param1=value1&param2=value2" 
url = url.replace("&param1=value1", "");

Upvotes: 1

Umesh Sehta
Umesh Sehta

Reputation: 10683

try this:-

function RemoveParam(url, p) {
   return url
  .replace(new RegExp('[?&]' + p + '=[^&#]*(#.*)?$'), '$1')
  .replace(new RegExp('([?&])' + p + '=[^&]*&'), '$1');
}

var url='www.mydomain.com/default.aspx?param3=value3&param1=value1&param2=value2'

 alert(RemoveParam(url,'param1'));

Demo

Upvotes: 1

Related Questions