I don’t know if it makes sense what you want to do, but it is possible to pass the parameter text
to the URL you are using.
<a href="https://twitter.com/compose/tweet?text=Olá Mundo" target='_blank'>
Tweetar 'Olá Mundo'
</a>
How to change the value of href
at runtime?
You can change anything (or practically) on your web page using Javascript. In your case, you need to follow these 2 simple steps:
- Get the element
a
page using some API for this - document.getElementById
, document.querySelector
and so on...
- Change the property
href
of this element for the value that the variable contains.
Example:
const urlBase = "https://twitter.com/compose/tweet"
const texto = "Meu tweet"
var btn = document.querySelector('#btn-tweet');
btn.addEventListener('click', () =>
{
const elementoAncora = document.querySelector('a')
elementoAncora.href = `${urlBase}?text=${texto}`
});
<button id="btn-tweet">Alterar texto do tweet</button>
<br>
<a href="https://twitter.com/compose/tweet?text=Olá Mundo" target='_blank'>
Tweetar
</a>
For the code to be useful even you will need to get this text from somewhere, but it gets task =)
It is almost that, but this way will always be published 'Hello World'. If the variable changes, the link will still load the message 'Hello World', not the contents of the variable.
– Guilherme Arruda
@Guilhermearruda Okay, but you’re a programmer, right? Now just use your skills to change the value of
href
. My contribution was to inform you of the parametertext
=)– Jéf Bueno
I’m a student, I’m trying to make a point, I still don’t know how to change the
href
to do what I’m looking for. If you can leave some useful article I would appreciate it.– Guilherme Arruda
@Guilhermearruda Okay, I tried to show you in the answer what needs to be done to use the value of the variable in the URL, I hope it was clear xD
– Jéf Bueno
Thanks, I could understand better now. It became easier to adapt also the way I need. Thanks again.
– Guilherme Arruda
@Guilhermearruda Tranquilo, young man. Good luck in your studies =D
– Jéf Bueno