Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get/change/remove URL parameters with jQuery?

I was looking over this question and searching google but I didn't find any update, so I am wondering if any of you know anything more recent because the last update on

https://github.com/blairmitchelmore/jquery.plugins was in 2009 and 2010 on https://github.com/cowboy/jquery-bbq

Or any other ideas? I need to add/change/remove parameters to my url

like image 950
Alex Avatar asked Dec 28 '22 07:12

Alex


1 Answers

Its easy to do by pure JS.

See this code from www.samaxes.com

var queryParameters = {}, queryString = location.search.substring(1),
    re = /([^&=]+)=([^&]*)/g, m;

while (m = re.exec(queryString)) {
    queryParameters[decodeURIComponent(m[1])] = decodeURIComponent(m[2]);
}

// Add new parameters or update existing ones
queryParameters['newParameter'] = 'new parameter';
queryParameters['existingParameter'] = 'new value';
location.search = $.param(queryParameters);

Its not flawless. But at least it can give you some idea.

Update 1:
Here is a function I wrote for another answer (cant remember). It works perfect.

like image 185
Shiplu Mokaddim Avatar answered Jan 05 '23 09:01

Shiplu Mokaddim