Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript / jQuery get all url parameters and add/change one

I have the following url

http://www.domain.com?page=options&tab=general

I want to be able to get to the tab parameter and change it e.g

I have two tabs General & Styles and when I click on styles, I want a hidden field(that has this url as a value) to change so that it reads the same url but with the tabs parameter changed to styles. So it would look like this

http://www.domain.com?page=options&tab=styles

However, this url may not have the parameter tab when the page is loaded so i need to be able to add a parameter to the url query string.

There will be many more tabs so I cannot just replace the text general with styles

Anyone know? Thanks

like image 896
Carl Thomas Avatar asked Aug 25 '12 12:08

Carl Thomas


1 Answers

var s = "http://www.domain.com?page=options&tab=general"
var queryString = s.substring(s.lastIndexOf("?") + 1);
var newQueryString = $.map(queryString.split("&"), function(pair) { 
  var p = pair.split("="); 
  if (p[1] == "general") { 
    p[1] = "styles";
    return p.join("=");
  } else { 
    return pair;
  } 
}).join("&");
like image 179
João Silva Avatar answered Oct 11 '22 18:10

João Silva