mbleigh / jquery-pageselect

A jQuery plugin to handle selection of text on the page (not in a textarea).

This URL has Read+Write access

jquery-pageselect / jquery.pageselect.js
100644 33 lines (31 sloc) 0.91 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/* jQuery PageSelect
* Version 0.0.1
* Written by Michael Bleigh (http://www.mbleigh.com/) of Intridea, Inc. (http://www.intridea.com)
* @requires jQuery >= 1.2
*
* Copyright 2009 Michael Bleigh
* Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php
*
*/
 
 /*
* @description A plugin to handle the selection of HTML on the page in a cross-browser compatible manner.
*/
jQuery(function () {
  jQuery(window).mouseup(function(e) {
    var selection = jQuery.getSelection();
    if (selection.length > 0) {
      jQuery(e.target).trigger('pageselect', [selection]);
    }
  });
});
 
jQuery.getSelection = function() {
  if(window.getSelection){
    return window.getSelection().toString();
  }
  else if(document.getSelection){
      return document.getSelection() + "";
  }
  else if(document.selection){
      return document.selection.createRange().text + "";
  }
}