0

I am writing a drag and drop script. The problem when holding down the mouse and moving it along the page is that it starts selecting the text. I found a way to prevent this by calling...

var selection = window.getSelection();
selection.removeAllRanges();

...when moving the mouse. As usual, I am having trouble with Internet Explorer. Is there an equivalent to make this method work in <IE9 (No plug-ins, pure JavaScript)?

Thanks in advance!

Ood
  • 1,445
  • 4
  • 23
  • 43
  • possible duplicate of [getSelection() not working in IE](http://stackoverflow.com/questions/5421892/getselection-not-working-in-ie) – Lesha Ogonkov Oct 08 '14 at 17:21

2 Answers2

2

The entire selection model is totally different in older IEs. There's no window.getSelection() nor Range objects like in modern browsers. You've to get familiar with TextRange Objects etc. when implementing selections and ranges in IE<9.

However, your problem with IE can be solved in a simple way. Attach onselectstart event to window (or any element you need). In a handler prevent the default action and cancel bubbling. Something like this:

window.attachEvent('onselectstart', function (e) {
    e.returnValue = false;
    e.cancelBubble = true;
    return false;
}
Teemu
  • 22,918
  • 7
  • 53
  • 106
2

The equivalent code in old IE (up to and including version 8) is

document.selection.empty();

Relevant MSDN page: http://msdn.microsoft.com/en-gb/library/ie/ms535869(v=vs.85).aspx

Tim Down
  • 318,141
  • 75
  • 454
  • 536