Pergunta

Here's what I have

if(condition1) {
     location.href = location.href+'/?site_type=normal';
}
else if(condition2) {
    location.href = location.href+'/?site_type=other';
}

Of course, if the location.href already has query vars on it, thats a problem, etc.

I need to

  1. Find the vars from the query string
  2. if site_type already exists, replace the value with either 'normal' or 'other'
  3. rebuild the url with the new site_type

edit: I found I needed to account for all kinds of URLs:

  • domain.com
  • domain.com/path/to/sth/
  • domain.com/?site_type=normal
  • domain.com?var=123&foo=987
  • domain.com/path/?site_type=normal&var=123&foo=987

So, here's what I came up with, suggestions welcome:

var searchstring = window.location.search;
var url = window.location.href;

console.log('search: ' +  searchstring);
console.log( 'url: ' +  url);
// strip search from url
url = url.replace(searchstring,"");
console.log( 'url: ' +  url);
//strip site_type from search
searchstring = searchstring.replace("&site_type=normal","")
                        .replace("&site_type=other","")
                        .replace("?site_type=normal","")
                        .replace("?site_type=other","")
                        .replace("?","")
                        ;
console.log('search: ' +  searchstring);
if(searchstring != ''){searchstring = '&' + searchstring;}
var final = url + '?site_type=normal' + searchstring;
final = final.replace("&&","&");
console.log('final: ' +  final);
Foi útil?

Solução

You can directly access the query string with window.location.search. You can convert it to an object using this regex trick found here.

var queryString = {};
window.location.search.replace(/([^?=&]+)(=([^&]*))?/g, function($0, $1, $2, $3) {
  queryString[$1] = $3; }
);

Then set the site_type on queryString appropriately.

queryString["site_type"] = "normal";

And finally, convert it back into a string and set that as the window.location.search.

var searchString = "";
for ( q in queryString ) {
  searchString+="&" + q + "=" + queryString[q];
}
window.location.search = searchString;

Outras dicas

Here's a way to do this:

//remove existing param and append new one..
var newHref = window.location.href.replace(window.location.search,"") + '?site_type=other';

//change href
window.location.href = newHref;

works only if you have one parameter that you want to replace, otherwise it would remove all parameters.

for example if you have

yourpage.com/?site_type=normal

and you need only website not query vars you cans clear them

var novars= location.href.replace(window.location.search,"")

this case novars = youroage.com

for just getting variables u can do this:

var site_type = window.location.search.replace("?site_type=","");

here i will get site_type value whether its normal or other

this case your variable site_type = "normal"

for rebuilding url u can just add new site_type

location.href = novars+"?site_type=normal"

or

location.href = novars+"?site_type=other"
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top