Navigating between Javascript Urls (changing url value)

Asked

Viewed 315 times

0

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

index.html

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

html indexabcd.

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

Joao.html

With the command would be:

joaoabcd.html

I thank those who help

2 answers

0


This should take care of.

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>

0

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

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

Upshot:

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

Browser other questions tagged

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