Gus Powell
Gus Powell

Reputation: 53

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

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>

Upvotes: 0

Views: 1394

Answers (2)

chantellebradley
chantellebradley

Reputation: 55

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

Source

Upvotes: 0

David Hedlund
David Hedlund

Reputation: 129782

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'

Upvotes: 2

Related Questions