I have a function like this:
function tweet(){
window.open("https://twitter.com/intent/tweet?text=My text. #myHashtag"
);
}
But JavasScript stops at the # sign. How to write it?
I have a function like this:
function tweet(){
window.open("https://twitter.com/intent/tweet?text=My text. #myHashtag"
);
}
But JavasScript stops at the # sign. How to write it?
Use %23
which an encoded form of #
.
If this is coming from the user, you really should use encodeURIComponent
before putting it into the query string.
Based on twitter doc, you can pass hashtags as &hashtags=
https://twitter.com/intent/tweet?text=My+text&hashtags=bransonpickel
Encode the (input) text:
let input = "My text. #myHashtag";
let baseUrl = "https://twitter.com/intent/tweet?text=";
// window.open(baseUrl + encodeURIComponent(input));
console.log(baseUrl + encodeURIComponent(input));