Javascript / jQuery get all URL parameters and add / change

I have the following url

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

I want to be able to go to the tab option and change it, for example.

I have two tabs โ€œGeneralโ€ and โ€œStylesโ€, and when I click styles, I want the hidden field (having this URL as a value) to change so that it reads the same URL, but with tab parameter changed to styles. So it will look like

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

However, this URL may not have a options tab when loading the page, so I will need to add the parameter to the url query string.

There will be more tabs, so I canโ€™t just replace the text with general styles

Somebody knows? Thanks

+5
source share
1 answer
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("&");
+7
source

All Articles