document.addEventListener("touchmove") vs document.ontouchmove

Viewed 1012

I am recently run into a problem to prevent touch move event on the browser.

I have done it by document.ontouchmove

but i wasn't been able to do the same with document.addEventListener("touchmove")

just wondering what's the difference between two.

why first one works on the mobile but addEventListner don't.

1 Answers

May be the case.

// Case 1
document.ontouchmove = function (e) {
  // Will prevent default action
  e.preventDefault();
};

// Case 2
document.ontouchmove = function () {
  // Will prevent default action
  return false;
};

// Case 3
document.addEventListener('touchmove', function (e) {
  // Will prevent default action
  e.preventDefault();
});

// Case 4
document.addEventListener('touchmove', function () {
  // WILL NOT prevent default action
  return false;
});

Related