简体   繁体   中英

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 eg

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

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("&");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM