Post a preset phrase on Twitter

Asked

Viewed 37 times

-1

const frase = 'Olá Mundo!'

<a href="https://twitter.com/compose/tweet" target='_blank'>
    <button>Twitter</button>

The link to the tag <a> redirects to the Twitter page already with 'What’s happening? ' open, for the user to make a publication. Is there any way that link loads the previously defined phrase, causing the user not to have to type 'Hello World!'?

1 answer

0


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:

  1. Get the element a page using some API for this - document.getElementById, document.querySelector and so on...
  2. 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.

  • @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 parameter text =)

  • 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.

  • @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

  • Thanks, I could understand better now. It became easier to adapt also the way I need. Thanks again.

  • @Guilhermearruda Tranquilo, young man. Good luck in your studies =D

Show 1 more comment

Browser other questions tagged

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