How to generate a "token" in the link bar with javascript?

Asked

Viewed 1,434 times

1

I wanted to know if you can generate random characters in the link on a page, without having to change the address of the page.

For example, when the person clicks on the link, they will be sent to:

site.com/pages/pagina-de-destino.html

but I want the browser to display in the search bar:

site.com/pages/pagina-de-destino.html#access_token=(por volta de 160 caracteres aleatórios)

How to do this in Javascript/HTML?

1 answer

4


Within what was asked, I think it solves:

var result = '';
for (var i = 80; i > 0; --i) result += (Math.floor(Math.random()*256)).toString(16);

document.getElementById( 'link' ).href += '#access_token=' + result;
Ponha o mouse sobre o link e olhe a barra de status:<br>
<a id="link" href="/a/107323/70">Link</a>

Just remember that anything in JS can be manipulated on the client side.

  • My friend, how and where do I apply this js on the page? Sorry, but I’m new to Javascript. I don’t know if it affects, but in the project, the link is inside the div "item", which is inside the div "main"

  • Simply put the same name in the link and getElementById ID, no matter where you are. JS can be put where you want from the page too, or externally loaded, as long as it is executed.

Browser other questions tagged

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