0

I thought the following code targets both IE and Opera

window.onload = function() {
    document.attachEvent("onmousewheel", function() {
        console.log("mousewheel detected");
    });
};

However, Opera (11.11) has no response at all.

I'v tried to Google this but didn't get any helpful info. Does Opera even support mousewheel event?

Combinations I'v tried...

attachEvent, addEventListener and mousewheel, onmousewheel, DOMMouseScroll

update

ok, mousewheel event does get caught on Opera, the problem is that my Opera's console does not print anything out. alert works.

user1643156
  • 4,407
  • 10
  • 36
  • 59
  • [`addEventListener()`](https://developer.mozilla.org/en-US/docs/Talk:DOM/element.addEventListener)? – PeeHaa Apr 17 '13 at 20:00
  • @PeeHaa埽 isn't `addEventListener` for FF and Webkit? – user1643156 Apr 17 '13 at 20:03
  • Nope. `addEventListener` works on all decent browsers – PeeHaa Apr 17 '13 at 20:05
  • @user1643156 Nope. `addEventListener` is [the standardized method](http://dev.w3.org/2006/webapi/DOM-Level-3-Events/html/DOM3-Events.html#events-EventTarget-addEventListener) and [Opera has supported it for a while](https://developer.mozilla.org/en-US/docs/DOM/EventTarget.addEventListener#Browser_compatibility). – Jonathan Lonowski Apr 17 '13 at 20:06
  • well, how about a solution for older versions of Opera? – user1643156 Apr 17 '13 at 20:09
  • @user1643156 opera 7 is from 2003. Are you really supporting 10 year old browsers? That sucks ;-) – PeeHaa Apr 17 '13 at 20:10

1 Answers1

0

You need to handle compatibility for all browsers and not switch to a solution that could break others. Simplest way of validating whether or not you can use a solution is "if".

In this case you can use 2 solutions. addEventListener & attachEvent.

var mousewheelevt=(/Firefox/i.test(navigator.userAgent))? "DOMMouseScroll" : "mousewheel" //FF doesn't recognize mousewheel as of FF3.x

if (document.attachEvent) //if IE (and Opera depending on user setting) {
    document.attachEvent("on"+mousewheelevt, function(e){alert('Mouse wheel movement detected!')})
else if (document.addEventListener) { //WC3 browsers
    document.addEventListener(mousewheelevt, function(e){alert('Mouse wheel movement detected!')}, false)

Source

James_1x0
  • 931
  • 10
  • 20
  • that is the code I'm using here. see my update, it's becasue Opera's console not printing anything out, my bad. thanks anyway. – user1643156 Apr 17 '13 at 20:20