I needed to write a quick bit of code today to track when a user selects any text on a page. The key was to attach a handler to the "mouseup" event and grab the selection data. This was plain 'ole JS and thankfully <a href="http://dean.edwards.name">Dean Edwards</a> and <a href="http://crisp.tweakblogs.net/">Tino Zijdel</a> had created a nice <a href="http://therealcrisp.xs4all.nl/upload/addEvent_dean.html">cross-browser event handler routine</a> which worked great. And David Walsh has a nice post on similar functionality using MooTools & jQuery so I was able to leverage some of that code that deals with grabbing the selected text:

[code lang="js"]
function findSelected() {
	if(window.getSelection) { 
		return window.getSelection(); 
	}
	else if(document.getSelection) { 
		return document.getSelection(); }
	else {
		var selection = document.selection &amp;&amp; document.selection.createRange();
		if(selection.text) { 
			return selection.text; }
		return false;
	}
	return false;
}

function getSelectedText() {
	var selected = findSelected();
	if(selected &amp;&amp; (selected = new String(selected).replace(/^\s+|\s+$/g,''))) {
		alert( selected );
	}
}
[/code]