function getSelectionText() { var text = ""; if (window.getSelection) { text = window.getSelection().toString(); } else if (document.selection && document.selection.type != "Control") { text = document.selection.createRange().text; } return text; } $(document).ready(function () { $('div,p').mouseup(function (e) { if (getSelectionText() != "") { window.open('https://twitter.com/intent/tweet?text='+encodeURI(getSelectionText()) + '&url=' + encodeURI(document.URL)); } }) });
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter