Event.returnValue = false does not work

I am working on a scrollbar in Javascript. Everything works fine except for one problem. I notice that when you drag the scroll bar, if I move the mouse around the context that scrolls, the content is selected. I do not want this to happen, so I used the preventDefault method from the event object, which worked fine for IE9 and other modern browsers. But in IE7 and IE8 the problem persists. I did a few searches and found that I had to set the returnValue parameter of the returnValue object to false. But the problem still persists. Also, if I write alert(window.event.returnValue) , it appears undefined .

 scrollbar.onmousedown = function (event) { if (typeof event == 'undefined') event = window.event; if (typeof event.preventDefault != 'undefined') event.preventDefault(); event.returnValue = false; // do some stuff } 

What am I doing wrong?

+4
source share
3 answers

IE7 & 8 does not have an event object as a function parameter; instead, window.event exists. Try

 window.event.cancelBubble = true 

to stop the spread. To avoid problems with FireFox, etc., follow these steps:

  if (!event) event = window.event; //IE9 & Other Browsers if (event.stopPropagation) { event.stopPropagation(); } //IE8 and Lower else { event.cancelBubble = true; } 
+2
source

I fixed my problem at the end by adding event.returnValue = false to the onmousemove event, instead to the onmousedown event, and it worked. It does not answer the question of why the original code is wrong, but I wanted to publish it for people who see my question so as not to waste time helping me with a problem that I have already fixed. Thanks for your quick answers, I appreciate.

0
source

I had the same problem too. preventDefault did not work in IE. so I added the code below to stop the spread

 if (a_event.preventDefault) { a_event.preventDefault(); } else if (typeof a_event.returnValue !== "undefined") { a_event.returnValue = false; } 
0
source

Source: https://habr.com/ru/post/1492519/


All Articles