benmajor / jQuery-Touch-Events

A collection of mobile event plugins for jQuery.
MIT License
699 stars 225 forks source link

[1.09] Triggering `mousedown` causes error #138

Closed imbrish closed 6 years ago

imbrish commented 7 years ago

Executing the following code:

$('body').on('swipe', function () {});
$('body').trigger('mousedown');

Causes an error:

Uncaught TypeError: Cannot read property 'targetTouches' of undefined
    at HTMLBodyElement.touchStart (jquery.mobile-events.js:545)
    at HTMLBodyElement.dispatch (jquery.min.js:3)
    at HTMLBodyElement.r.handle (jquery.min.js:3)
    at Object.trigger (jquery.min.js:4)
    at HTMLBodyElement.<anonymous> (jquery.min.js:4)
    at Function.each (jquery.min.js:2)
    at n.fn.init.each (jquery.min.js:2)
    at n.fn.init.trigger (jquery.min.js:4)
    at <anonymous>:1:11
touchStart @ jquery.mobile-events.js:545
dispatch @ jquery.min.js:3
r.handle @ jquery.min.js:3
trigger @ jquery.min.js:4
(anonymous) @ jquery.min.js:4
each @ jquery.min.js:2
each @ jquery.min.js:2
trigger @ jquery.min.js:4
(anonymous) @ VM8570:1

Easiest to try by opening the developer console (ctrl+shift+i on Chrome) on a page with jquery.mobile-events.js loaded and using the above code.

Example of the error can be found in this fiddle.

Happens also for mousemove event, and possibly other.

benmajor commented 7 years ago

mousedown and mousemove are not events defined by this library? Furthermore, my test through jsFiddle doesn't seem to replicate the issue:

https://jsfiddle.net/ydftayb9/

imbrish commented 7 years ago

I modified your example so that error occurs. I didn't mention originally that the element, for which we trigger the mousemove event, has to have some touch handler registered, for example swipe.

https://jsfiddle.net/ydftayb9/4/

Updated the top post as well.

imbrish commented 7 years ago

The error is obviously caused by e.originalEvent being null for fake events. Returning from the handler when originalEvent is missing should be good enough solution.

If you cannot fix this now I could start a pull request, but It'll take me some time to investigate the code.