4

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

5
  • You'd like to alter the URL after having a loaded page? Commented Jan 25, 2012 at 20:01
  • yes... exactly, after the page has been loaded Commented Jan 25, 2012 at 20:02
  • This is tough to do, mainly because of the security issues involved. Are you unable to use either of the plugins you linked to? I'd imagine these would only manipulate the hash value. Commented Jan 25, 2012 at 20:07
  • i didn't tried them out because I am looking first for a more recent version to see if something... if not than I will see if they work. From 2009 to 2012 something better could've appeared Commented Jan 25, 2012 at 20:12
  • Manipulating query parameters is such a basic thing that it doesn't hardly matter whether the code is brand new or several years old. Commented Jan 25, 2012 at 20:15

1 Answer 1

10

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.

Sign up to request clarification or add additional context in comments.

7 Comments

Wouldn't location.search reload the page? I suppose this is Ok if that works for the op. I figured that a reload was undesirable.
No. location.search contains the query string with ? added. Its a string.
Typing location.search = "foobar" into the console causes a page reload.
@hackNightly Yes. it'll reload the page. In fact OP want to change the param of query string so that it reloads.
Oh, ok, that makes more sense. Good call.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.