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');"

Community
  • 1
  • 1
Draven
  • 1,467
  • 2
  • 19
  • 47
  • have you tried searching? the answer can be found [here](http://stackoverflow.com/q/1090948/944681) – Michal Klouda Oct 23 '12 at 08:04
  • 2
    With pure JS check [this answer](http://stackoverflow.com/questions/9048360/how-to-extract-relative-url-from-argument-values-from-request-string/9048444#9048444) too – Shiplu Mokaddim Oct 23 '12 at 08:06
  • Yes, of course I searched. Just used the wrong search query. Thank you. – Draven Oct 23 '12 at 08: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. – Draven Oct 23 '12 at 08: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. – Shiplu Mokaddim Oct 23 '12 at 08:22

1 Answers1

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");
    });
});
VisioN
  • 143,310
  • 32
  • 282
  • 281