Skip to content

Instantly share code, notes, and snippets.

@a-laughlin
Created October 20, 2013 04:19
Show Gist options
  • Save a-laughlin/7065031 to your computer and use it in GitHub Desktop.
Save a-laughlin/7065031 to your computer and use it in GitHub Desktop.
Simple utility to get selected text in a page
;(function(win,doc){
function _getSelectedText (){
_getSelectedText = win.getSelection ? // redefine itself when first run
win.getSelection :
doc.getSelection ?
doc.getSelection:
doc.selection ?
function(){return doc.selection.createRange().text;} :
function(){return '';};
return _getSelectedText();
}
function _deselectText() {
_deselectText = win.getSelection ? // redefine itself when first run
function(){win.getSelection().removeAllRanges();} :
doc.getSelection ?
function(){doc.getSelection().removeAllRanges();} :
doc.selection ?
function(){doc.selection.empty();} :
function(){};
return _deselectText();
}
})(window, document);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment