Linkify Twitter feed (Usernames, hashtags and links)


/ Published in: JavaScript
Save to your folder(s)

This will let you use the linkify_tweet() function on any string to make @username, #hashtags and http://any.links be clickable in your API-received twitter feeds.


Copy this code and paste it in your HTML
  1. String.prototype.linkify_tweet = function() {
  2. var tweet = this.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/g, function(url) {
  3. var wrap = document.createElement('div');
  4. var anch = document.createElement('a');
  5. anch.href = url;
  6. anch.target = "_blank";
  7. anch.innerHTML = url;
  8. wrap.appendChild(anch);
  9. return wrap.innerHTML;
  10. });
  11. tweet = tweet.replace(/(^|\s)@(\w+)/g, '$1@<a href="http://www.twitter.com/$2" target="_blank">$2</a>');
  12. return tweet.replace(/(^|\s)#(\w+)/g, '$1#<a href="http://search.twitter.com/search?q=%23$2" target="_blank">$2</a>');
  13. };

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.