0

Possible Duplicate:
Change URL parameters with jQuery?

Currently, I have a <div> tag that looks like a button and changes the URL with an onClick function which redirect to the same page just adds &view_all=Yes or &view_all=No to the URL.

<div onclick="javascript:window.location.href = 'page.php?action=list-volunteer-apps&view-all=No';">Hide Closed Applications</div>

Well, I am adding a new feature to the website and I need to be able to append that &view_all=Yes or &view_all=No to the end of the URL instead of redirecting them because I'll have other variables in the URL that I can't lose.

I have figured out a way to do the append but it keeps adding the &view_all variables to the end of URL so it looks like this page.php?action=list-volunteer-apps&view-all=No&view_all=Yes&view_all=No.

This is the way I am doing the append: onclick="javascript:window.location.assign(window.location.href+='&view-all=No');"

5
  • have you tried searching? the answer can be found here Commented Oct 23, 2012 at 8:04
  • 2
    With pure JS check this answer too Commented Oct 23, 2012 at 8:06
  • Yes, of course I searched. Just used the wrong search query. Thank you. Commented Oct 23, 2012 at 8:06
  • @shiplu.mokadd.im Could you give me an example on how to use that to solve my problem? I don't know JS that well. Commented Oct 23, 2012 at 8:15
  • Using that function you can get an object or associated array of url parameters. After that you have to add/modify new value to view_all variable. Then build the new url. Its the opposite of that function. Commented Oct 23, 2012 at 8:22

1 Answer 1

0

You can use regular expression to replace the value in a string:

$(this).attr("onclick", function(i, val) {
    return val.replace(/view-all=(Yes|No)/, function() {
        return "view-all=" + ((arguments[1] || "") == "Yes" ? "No" : "Yes");
    });
});
Sign up to request clarification or add additional context in comments.

3 Comments

Could you tell me how I would use that exactly?
@Draven For instance, like that: jsfiddle.net/mx7s8.
Well, seems like a good idea but I just don't know enough JS to get it to work.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.