1

Is there any way in Javascript to tell what the most recently-fired event was? Reason I'm asking is I'm working on a site that isn't behaving properly on one of those new Ultrabooks that's running Windows 8 and is a laptop with a touch screen. If you use the standard mouse functionality (with a touchpad or an actual mouse), things work fine, but if you use the touch screen, things don't.

This only happens with IE; Chrome has its own issues (which I have fixed in the code), and Firefox hasn't given us any problems.

Basically, the functionality we have includes a "hoverIntent" block, and if you use the touch screen on IE, it registers both the "over" and "out" functions, which is a problem.

However, if there was a way for me to tell whether the last thing that happened was that the user TOUCHED THE SCREEN or CLICKED WITH A MOUSE, I'd have a solution in place. But I couldn't tell if there's a way to do that.

The only thing I could find was tacking on ".data('events')" on an element, but what returns is "click" regardless of whether it was an actual mouse click or a tap on the screen.

Is there a way to do this?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
dauber
  • 330
  • 6
  • 20

3 Answers3

1

The browser does not have a standard way of recording events that happened previously. If you want to know what events happened prior to the current event, then you will have to install an event handler for those events and record them yourself so you can then look back at them at some future time.

For events that propagate, you could install some event handlers on the document object and record both event and target for the last N events.

If you're just trying to figure out what event the current event is, then you can examine the type property of the event object that is passed into the event handler as in e.type.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
0

You can add an event to your function arguments and then use event.type to check which event is triggered. ex:

var x = function(e) { 
    alert(e.type);
}
Hessam
  • 1,377
  • 1
  • 23
  • 45
  • Yeah, that's what I tried, but it returns "click" whether it's a screen tap or a mouse click. :( Thanks, though. – dauber Nov 26 '13 at 15:40
  • @dauber Have you tried jQTouch? As far as I know it has different events for tap and click – Hessam Nov 26 '13 at 19:27
0

So I found out that IE has a completely different set of touch events from what EVERY OTHER BROWSER IN THE UNIVERSE has. ughhh. Instead of "touchstart," you use "MSPointerDown," etc. My solution was basically to write new event handlers for MSIE's touch device events.

dauber
  • 330
  • 6
  • 20