Javascript URL redirection code

4

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

My need is as follows:

When the user accesses a certain page, I would like to supplement the URL with some parameters, eg:

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

Remembering that it can also enter a subdirectory, for example:

xxx.com.br/pasta/index.php, you should also add xxx.com.br/pasta/index.php&variavel=1

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

Important detail, I did this and it was looped, because when redirecting it it identifies the domain in the same way, so if the domain hits, it must have another check to see if the parameter does not already exist in the URL. p>     

asked by anonymous 05.12.2014 / 14:40

4 answers

4

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

So you can have the following script on every page that needs this functionality:

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

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

So your complete code could be:

if (location.hostname == 'xxx.com.br') location.search = location.search || '?variavel=1';
    
05.12.2014 / 15:08
2

This is a function that already tests whether the parameter exists in the original URL, and if it does not exist, it 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>';
    
05.12.2014 / 16:12
1

This way you can get the entire 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
    
05.12.2014 / 14:54
0

Just check and validate the user's host and url (href), so add it if domain is equal to x , but do not add it if it already exists:

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';
}

Then just add the onload=validaDominio('xxx') parameter to <body> of the pages you want to check, with:

<body onload=validaDominio('xxx') >
    
05.12.2014 / 14:55