O'Reilly logo

iPhone JavaScript Cookbook by Arturo Fernandez Montoro

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Preventing the default behavior for events

Remember that our applications are run in Safari, which is a web browser and events are handled in a different way than in native applications. In order to implement interfaces for our web applications that are similar to native ones, we'll need to disable the default behavior for these kind of events. This recipe explains how to prevent such behaviors.

How to do it...

  1. First, we need to write one JavaScript function as follows:
    function avoid_behavior(evt) {
    evt.preventDefault();
    }
    
  2. The second step will be to write some code for invoking the previous JavaScript function. Actually, this function will be called for different events. Use the following<body> tag for the HTML pages of your application:
    <body ontouchmove="avoid_behavior(event);" ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required