3

I'm looking for the simplest way to add parameters to a URL and then reload the page via javascript/jquery. I'm trying to avoid any plugins. Essentially I want:

http://www.mysite.com/about

to become:

http://www.mysite.com/about?qa=newParam

or, if a parameter already exists, then add a second parameter:

http://www.mysite.com/about?qa=oldParam&qa=newParam
asked Mar 10, 2014 at 21:44

3 Answers 3

3

Here is a vanilla solution, it should work nicely for all cases (except wrong inputs of course).

function replace_search(name, value) {
 var str = location.search;
 if (new RegExp("[&?]"+name+"([=&].+)?$").test(str)) {
 str = str.replace(new RegExp("(?:[&?])"+name+"[^&]*", "g"), "")
 }
 str += "&";
 str += name + "=" + value;
 str = "?" + str.slice(1);
 // there is an official order for the query and the hash if you didn't know.
 location.assign(location.origin + location.pathname + str + location.hash)
};

EDIT: if you want to add stuff and never remove anything the function is way smaller. I'm not very found of having multiple fields with different values but there is no specifications on that.

function replace_search(name, value) {
 var str = "";
 if (location.search.length == 0) {
 str = "?"
 } else {
 str = "&"
 }
 str += name + "=" + value;
 location.assign(location.origin + location.pathname + location.search + str + location.hash)
};
answered Mar 10, 2014 at 22:23
Sign up to request clarification or add additional context in comments.

1 Comment

This worked great. I added an if/else statement to detect if a parameter is already present. Thank you so much.
3

Have a look at Window.location (MDN) for information on window.location.

A quick and dirty solution is:

location += (location.search ? "&" : "?") + "qa=newParam"

It should work for your example, but misses some edge cases.

answered Mar 10, 2014 at 22:08

Comments

1

location.href will give you the current URL. You can then edit your query string and refresh the page by doing something like this:

if (location.href.indexOf("?") === -1) {
 window.location = location.href += "?qa=newParam";
}
else {
 window.location = location.href += "&qa=newParam";
}
answered Mar 10, 2014 at 21:51

2 Comments

I attempted to use this, but for some reason it creates an infinite loop, constantly adding on the parameter. Not sure why though, as I would assume the conditional would handle this.
@cc-test could it be that you just have these lines bare in a <script> tag? In that case they would be executed when the page loads, causing a redirect. Is so, then try putting it in a function like @username’s solution and call it when you want to reload the page.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.