I have an application that passes any selected body text to a text-2-speech server (via ajax) when a "Say It" button in the parent window is clicked; the selected text is in an iframe.
The selection works on Firefox 3.6 and IE 8, but not Safari 5 (have not tried any others; thats our supported browser list). In Safari, whenever the button to perform the function is clicked, any selected text is lost.
I've tried adding this to the mouseup for the presentation iframe, found as a solution for losing selected text within a control,but it doesn't work for selected text in the body of the document:
$("#the_presentation").mouseup(function(e){
e.preventDefault();
});
The function that catches the selected text is this:
function getSelectedText(){
//Grab selected text
var _txt = '';
var _selection = null;
if(window.getSelection){
_txt = window.getSelection().toString();
}
else if(document.getSelection){ //deprecated in ff
_txt = document.getSelection()+'';
}
else if(document.selection){
_selection = document.selection.createRange();
if(_selection != null && _selection.text )
{
_txt = _selection.text;
}
}
return _txt;
}
Thanks for any insight you can provide!
*edit: I forgot to mention only happens on Mac safari - windows safari does not have the problem.