Javascript URL redirect code

Asked

Viewed 1,289 times

4

i need to develop a javascript code that is not working.

My need is this:

When the user accesses a given page, I would like to complement the URL with some parameters, example:

If the domain is xxx.com.br/index.php, redirect it to xxx.com.br/index.php&variable=1

Remembering that it can also enter a subdirectory, example:

xxx.com.br/folder/index.php, should also add xxx.com.br/folder/index.php&variable=1

Basically, if the domain coicidir, it adds a variable at the end.

Important detail, I did this and the same was in loop, because when redirecting it identifies the domain in the same way, therefore, if the domain hits, must have another check to see if there is no parameter in the URL.

  • 1

    Tried to document.URL ??

4 answers

4

Add only &variavel=1 to the URL seems wrong, in which case you should use the "query" string that starts with ?. That is to say using location.search.

So you can have the following script on every page that you need this feature:

location.search = location.search || '?variavel=1';

Combining this with the location.hostname can verify that the domain is correct because the location.hostname gives exactly that information.

So your complete code could be:

if (location.hostname == 'xxx.com.br') location.search = location.search || '?variavel=1';
  • 1

    I didn’t know the location.search, thank you!

2

Follows a function that already tests whether the parameter exists in the original URL, and if it does not exist, adds.

This function is prepared for cases where other specified values already exist, and keeps them as they were:

function setDefaultParameter( url, parameter, value ) {    
  if ( ("&"+url.split("?")[1]+"=").indexOf("&"+parameter+"=") < 0 ) {
    url += (url.split("?")[1]?'&':'?') + parameter + '=' + value;
  }
  return url;
}

Just call this way to set all the parameters you want:

novoUrl = setDefaultParameter( window.location.href, 'modelo', '1'    );
novoUrl = setDefaultParameter( novoUrl             , 'ano'   , '2014' );

// Redireciona só se mudou algo:
if ( novoUrl != window.location.href ) {
  window.location = novoUrl ;
}


Demonstration:

function setDefaultParameter( url, parameter, value ) {    
  if ( ("&"+url.split("?")[1]+"=").indexOf("&"+parameter+"=") < 0 ) {
    url += (url.split("?")[1]?'&':'?') + parameter + '=' + value;
  }
  return url;
}

// Sem nenhum parametro
var url = 'http://example.com';
url = setDefaultParameter( url, 'modelo', '1' );
url = setDefaultParameter( url, 'ano', '2014' );
document.body.innerHTML += url + '<br>';

// com parametro já existente
url = 'http://example.com?cor=azul';
url = setDefaultParameter( url, 'modelo', '1' );
url = setDefaultParameter( url, 'ano', '2014' );
document.body.innerHTML += url + '<br>';

// com um dos parâmetros já especificado sendo preservado
url = 'http://example.com?ano=1998';
url = setDefaultParameter( url, 'modelo', '1' );
url = setDefaultParameter( url, 'ano', '2014' );
document.body.innerHTML += url + '<br>';

  • @Fabiovieira If necessary, put a variant that overrides the parameter, even if it already exists.

1

This way you can take the whole URL and do the necessary checks:

switch(document.URL){
    case 'http://www.meudominio.com/index.html': 
        location.href='/index.html?parametros' 
        break;
    case 'http://www.meudominio.com/outra.html': 
        location.href='/outra.html?parametros' 
        break;   
  }
}
alert(document.URL); //apenas para ver o que tem em document.URL
  • But where is validating the dominion?

  • Just put in the case 'minhaURL'... validates the entire URL at once

0

Just check and validate the user’s host and url(href), so you should add if the domain is equal to x but not add if there is already:

function validaDominio(dominioX){
  var host         = document.location.host;
  var addr         = document.location.href;
  var dominioEnd   = host.indexOf('.');
  var dominio      = host.substring(0, dominioEnd);

  if (dominio == dominioX && (addr.indexOf('&variavel') == -1))
    document.location.href = document.location.href + '&variavel=1';
}

And then just add the parameter onload=validaDominio('xxx') in the <body> of the pages you want to check, getting:

<body onload=validaDominio('xxx') >
  • 1

    dominio == dominio ? Something wrong not? rsrsr

  • now that’s right, thank you @Antonyalkmim

Browser other questions tagged

You are not signed in. Login or sign up in order to post.