Can't add a hashtag # into my share twitter button

1.3k views Asked by At

I have a twitter share button on my page. I use this code because I want a custom icon. My problem is I can't seem to add a hashtag into the custom text. Where it says 'custom text' is where I input my tweet.

  <a class="icon-twitter" rel="nofollow"
       href="http://twitter.com/"
       onclick="popUp=window.open(
           'http://twitter.com/intent/tweet?text= custom text',
           'popupwindow',
           'scrollbars=yes,width=800,height=400');
     popUp.focus();
     return false">
     <i class="visuallyhidden"><img class="social-media" src="images/twitter.png"/></i>
  </a>
2

There are 2 answers

0
David Hedlund On

You need to encode the #. This is because the # will otherwise be treated as the hash part of the url, and not a part of the text querystring, just like a & would be interpreted as a separator between two querystring parameters, rather than a part of the value, unless you encode it to %26. If the "custom text" can be anything, use encodeURIComponent:

'http://twitter.com/intent/tweet?text=' + encodeURIComponent('#custom #text')

If the value will always be hard coded, replacing the # with %23 would do:

'http://twitter.com/intent/tweet?text=%23custom %23text'
0
chantellebradley On

This is better for sharing hashtags: https://twitter.com/intent/tweet?hashtags=Tag1,Tag2

Source