Navigating between URLs with Javascript (changing url value)

0

Good evening, I'm trying to make a javascript command that redirect to an html page and add some characters in the final text, eg:

  

index.html

After the command (take the previous value of the link and add + abcd at the end):

  

indexabcd.html

And if it was a page with another name, for example:

  

joao.html

With the command would be:

  

joaoabcd.html

Thanks to anyone who helps

    
asked by anonymous 14.01.2017 / 03:08

2 answers

0

This should resolve this.

var urlAnterior = document.referrer;
//remove o .html
urlAnterior = urlAnterior.replace('.html','');
var complementoURL = "abcd";

function comando(){
  window.location.href = urlAnterior+complementoURL+".html";
}
<button type="button" onclick="comando()">Comando</button>
    
14.01.2017 / 03:53
0

With a small combination of substrings you can, if all the links end with the file extension, just look for the last point and add the chosen text back

  • lastIndexOf : To see where the last point is.
  • Substring : To get everything before the last point.
  • Substr : To get the extension again, it could be the Substring again, but it is one less parameter.

Result:

function mudarLink (link, texto){ 
  ultimoPonto = link.lastIndexOf('.');
  return link.substring(0, ultimoPonto) + texto + link.substr(ultimoPonto)
}
    
14.01.2017 / 04:05