views:

807

answers:

2

I'm working on a image viewer that has a draggable scrollbar. When the user drags the scrollbar, some of the text on the webpage becomes selected. I've tried

window.getSelection().removeAllRanges();

but that doesn't seem to work in IE7/8. Also tried the

document.getSelection().removeAllRanges();

which seems to work just as "well" with IE7/8.

Are there any other ways of doing this? Don't know of any jQuery solutions, but if there are, let me know (:

EDIT: This is the context of the onmouse-event

$("#slideBar").mousedown(function(f) {
 mouseDown = true;
    some more code...
}

$(document).mouseup(function() {
 if (mouseDown) {
    window.getSelection().removeAllRanges();
       more code...
    }
}
+2  A: 

You could add some CSS (with jquery) to deal with this:

element {
  -moz-user-select: none;
  -webkit-user-select: none;
  user-select: none;
}

Anyway, for IE, you need to add a proprietary attribute (with jquery) on that element:

unselectable="on"
Ionuț G. Stan
does the attribute have to be added to the element that contains the text, or can it be for the parent element that holds all text elements? Because I can't seem to get it to work in IE setting unselectable on the element that holds the whole image viewer.
peirix
According to the MSDN docs, it's not possible (can't post the link, google "msdn unselectable"). I had no idea about this. Give it a try using BYK's method, it should work in IE.
Ionuț G. Stan
+4  A: 

Try returning false on the functions you use, especially for the "mousedown" event. Also returning false on document.onselectstart and document.ondragstart would help.

BYK
‘return false’ on your element's ‘ondragstart’ is essential for IE, and the same with ‘onmousedown’ for others. This is a better approach than trying to ‘removeAllRanges’.
bobince