Sep 052010
 

Till last month, I seldom worked with JavaScript. But these days I am working on a web-based product and can’t avoid learning JavaScript. So my first mini-adventure was to get the selected text on a web page, for which I got code from here . I loved the simplicity and power of JavaScript in this example.

<script type="text/javascript">
//<![CDATA[

document.onclick=getSelText;

function getSelText()
{
var txt = '';
if (window.getSelection)
{
txt = window.getSelection();
}
else if (document.getSelection)
{
txt = document.getSelection();
}
else if (document.selection)
{
txt = document.selection.createRange().text;
}
else return;
if(txt!='') alert(txt);
}

//]]
</script>

(Source of Code: http://www.codetoad.com/javascript_get_selected_text.asp )