5

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

1

1 Answer 1

8
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("&");
Sign up to request clarification or add additional context in comments.

1 Comment

Great solution! As a note: if you don't want to use a hardcoded url, you can retrieve it with jquery: var url = window.location.href;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.