Skip to content

Instantly share code, notes, and snippets.

@guillaumepiot
Last active December 16, 2015 13:29
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save guillaumepiot/5442196 to your computer and use it in GitHub Desktop.
Save guillaumepiot/5442196 to your computer and use it in GitHub Desktop.
SOCIAL - Twitter share
// We bind a new event to our link
$('a.tweet').click(function(e){
//We tell our browser not to follow that link
e.preventDefault();
//We get the URL of the link
var loc = $(this).attr('href');
//We get the title of the link
var title = encodeURIComponent($(this).attr('title'));
//We trigger a new window with the Twitter dialog, in the middle of the page
window.open('http://twitter.com/share?url=' + loc + '&text=' + title + '&', 'twitterwindow', 'height=450, width=550, top='+($(window).height()/2 - 225) +', left='+$(window).width()/2 +', toolbar=0, location=0, menubar=0, directories=0, scrollbars=0');
});
@NazarenoL
Copy link

I would suggest you to replace escape() for encodeURIComponent() as the first one is deprecated since ECMAScript v3, and also there are a few differences on which characters doesn't escape each one, being the second one the more appropiated for the value of a URl value (component)

@BryanBarrera
Copy link

Good Script. I updated this script to either include the URL of the link or the URL of the location.

See lines 7-10 to see what i am talking about - https://gist.github.com/BryanBarrera/a05f4a6beb00c0555fae

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment