preventDefault does not work in Browsers for Mobile

0

In computer browsers I can prevent the standard anchor action normally, but when I use it in most mobile browsers it does not work.

I need to click on the link the browser does not point to the URL of the link, just show in an alert what is the URL that is in the attribute href , the code I'm using follows: p>

$(".myGallery a").on("click touchstart", function(event){
  event.preventDefault();
  alert($(this).attr("href"));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script><divclass="myGallery">
  <a href="https://www.yourhtmlsource.com/images/media/banjotooiebig.jpg">
    <img src="https://www.yourhtmlsource.com/images/media/banjotooiesmall.jpg"alt="Image">
  </a>
</div>

I tried the following browsers for Android :

  • Chrome 70.0.3538.110
  • Chrome 34.0.1847.114
  • Opera Mini 35.0.2254.127755
  • CM Browser 5.22.17.0003
asked by anonymous 06.12.2018 / 20:36

1 answer

1

You can put a return false; at the end of the function of the event that the action of the element that triggered the event will not proceed.

Under normal circumstances, preventDefault() should work, as I tested here in the Chrome below mentioned in the question:

Code:

$(".myGallery a").on("click touchstart", function(event){
  event.preventDefault();
  alert($(this).attr("href"));
  return false;
});
    
06.12.2018 / 20:58