Leading space on text selection ignored in Firefox
I am trying to grab user selected text in a HTML document using javascript. I am using the following code to do it:
function getSelectedText() {
var txt = '';
if (window.getSelection) {
txt = window.getSelection();
} else if (document.getSelection) {
txt = document.getSelection();
} else if (document.selection) {
txt = document.selection.cre开发者_开发问答ateRange().text;
}
return txt.toString();
}
When I select a word with a leading space, it gets ignore in Firefox. For example, if my text is "Selection example| to| demo space issue" and I make the selection represented by |, which is actually " to" in this case, Firefox ignores the leading space and returns just "to". IE and Chrome does not seem to have this problem and work just fine.
I am trying to surround the selection with a html tag like bold or span which is creating a problem when the leading space is removed.
How to fix this issue?
精彩评论