Under Firefox, Safari, Opera, you can use window.getSelection(), refer to MDC.
Under IE, you can use document.selection.createRange().text, refer to MSDN
to put together:
function getSelectionText() {
if(window.getSelection) {
return window.getSelection().toString();
} else if(document.selection && document.selection.createRange) {
return document.selection.createRange().text;
}
return '';
}
Note: When the value in input[type=text] is selected, getSelection cannot obtain the selected value under Firefox and Opera, but there is no problem under Safari.