Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable longPress action on mobile with javascript

in this period,I make some new webApps, but I have a big problem with drag and drop. I write a file manager in javascript, but when on mobile (smartphone, tablet andorid or iOs) I try to work drag and drop, the phone show me the longPress menu (on folder icon for examample) for copy url or image. there is some way in JS to disable longPress on mobile?

load image via css, isn't a valid solutions for me.

like image 662
r1si Avatar asked Dec 28 '25 03:12

r1si


2 Answers

-webkit-touch-callout: none;                /* prevent callout to copy image, etc when tap to hold */
    -webkit-user-select: none;                  /* prevent copy paste, to allow, change 'none' to 'text' */
like image 173
Vinodh Avatar answered Dec 30 '25 15:12

Vinodh


check the value of navigator.userAgent and compare with android|iphone|ipad etc and in your java script you could do the following

function init() {
  onLongPress(document.getElementById('element'));
}

function onLongPress(node) {
  node.ontouchstart = nullEvent;
  node.ontouchend = nullEvent;
  node.ontouchcancel = nullEvent;
  node.ontouchmove = nullEvent;
}

function nullEvent(event) {
  var e = event || window.event;
  e.preventDefault && e.preventDefault();
  e.stopPropagation && e.stopPropagation();
  e.cancelBubble = true;
  e.returnValue = false;
  return false;
}
like image 37
blganesh101 Avatar answered Dec 30 '25 17:12

blganesh101



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!