Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

preventDefault() in event handler -> different behaviour #35

Open
ch-fb opened this issue Jan 18, 2022 · 2 comments
Open

preventDefault() in event handler -> different behaviour #35

ch-fb opened this issue Jan 18, 2022 · 2 comments

Comments

@ch-fb
Copy link

ch-fb commented Jan 18, 2022

i compared firefox (linux) and samsung internet, preventDefault does not do the same thing. I use a workaround:

ne.addEventListener('long-press', function(e){
   if(!this.havePointer)
     return;
   //e.preventDefault(); // works in firefox/linux: (pending) click is not fired,
                         // but not in samsung internet: new clicks (handlers are accumulated!) are suppressed.
   // workaround:
   function suppressEvent(e){
     document.removeEventListener('click', suppressEvent, true);
     e.stopImmediatePropagation();
     e.preventDefault();
     e.stopPropagation();
   }
   document.addEventListener('click', suppressEvent, true);
   document.addEventListener('pointerdown', function removeSuppressor(e){
                document.removeEventListener('pointerdown', removeSuppressor, true);
                document.removeEventListener('click', suppressEvent, true);
            }, true);

   // actual handler...
 });

ne.addEventListener('pointerleave', function(e){
   this.havePointer=false;
 });

 ne.addEventListener('pointerenter', function(e){
   this.havePointer=true;
 });
@john-doherty
Copy link
Owner

It's hard to read this without formatting. Please update or provide a jsfiddle example

@ch-fb
Copy link
Author

ch-fb commented Feb 1, 2022

hi john, sorry for coming back to this so late (i was away from the inet for weeks). actually, last time, i wanted to point out 2 separate issues, the workaround above takes care of both. the first is that preventDefault() does not the same thing in different browsers, the second issue is that if the event handler is attached to an element, the long-press event also fires when swiping over the element.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants