0

I wanna make the button to the tweet page and designate what text and what url and hashtags to tweet, I want to tweet with the line break

`

const sendText = "aa \n"
const sendURL = window.location.href;
window.open("https://twitter.com/intent/tweet?text=" + sendText + "&url=" + sendURL + "&hashtags=a,b");

`

I tried \n and br tag and [br /] but twitter still ignores me

Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75
Ropher
  • 9
  • 1
  • You need to URL-encode your text, so \n becomes %0A - try `encodeURIComponent(sendText)`. – cmbuckley Dec 02 '22 at 13:54
  • 1
    Instead of concatenating query parameters manually, please use the [`URL` API](//developer.mozilla.org/en/docs/Web/API/URL) and its `searchParams` property, which _correctly encodes_ parameters: `const url = new URL("https://twitter.com/intent/tweet"); url.searchParams.set("text", sendText); url.searchParams.set("url", sendURL); url.searchParams.set("hashtags", "a,b"); window.open(url);`. – Sebastian Simon Dec 02 '22 at 13:55

0 Answers0