alert() selected/highlighted text

by lasha

HTML

<div>This is some text. Try selecting me to play around with the results.</div>

JavaScript

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;
}

$(function (){
   $('div').mouseup(function (e){
       var selectedText = getSelectionText();
       if (selectedText){
           alert(selectedText);
       }
   })
});