window.getSelection on a jquery ui dialog does not return selection in Safari
I am using the jquery UI dialog ( http://jqueryui.com/demos/dialog/ ) to display a chunk of text. I want to select some text on this dialog box and manipulate it. Now my current code works on Chrome and Firefox but is not working on Safari. (Not tested on IE yet)
I get the selection and display it as follows:
selection = window.getSelection();
alert(selection.toString().length);
The alert returns zero on Safari. I am relatively new to javascript/jquery so any pointers开发者_C百科 will help! Thank you!
Answer: For anyone seeing this... when I used the event "mousedown" instead of "click" in the dialog box on safari it started working. Thanks everyone!
It looks like Safari works a little differently. The getSelection() is not returning a normal string object so not all the string methods work on the object it returns. This link has a work around http://joemaller.com/503/getselection-workaround/
It looks like it is a very old "bug" so there may be better advise on what to do instead out there.
If you are doing anything with selections, I would strongly recommend that you take a look at Tim Down's rangy library. This guy is the Range/Selection object guru. His library does all the nitty gritty work of implementing a cross-browser range selection library. The documentation is good and he is actively working on the project (v 1.1 came out a few days ago).
(edit: I am NOT affiliated with the project. I just use it and love it!)
精彩评论