www.webdeveloper.com
Results 1 to 6 of 6

Thread: onclick not working on iPhone

  1. #1
    Join Date
    Feb 2005
    Location
    Norwich, UK
    Posts
    138

    onclick not working on iPhone

    Hi,
    an onclick I'm using doesn't seem to work on iPhones, although it's fine elsewhere.

    My code is
    Code:
    document.onclick = function(){newTarg(speedS)};
    Any thoughts of a way round this? I thought maybe onchange would do it but not sure how to implement it?

    Cheers,

    Derek
    _____________________________________________

    http://www.djillustration.com

  2. #2
    Join Date
    Dec 2012
    Posts
    97
    Click events don't work on iOS, you'd have to use the touchstart, touchmove and touchend events to work out if the users tapped the screen.

    Code:
    <script>
       var tap = true;
       document.addEventListener('touchstart',function(e) {
          tap = true;
       });
       document.addEventListener('touchmove',function(e) {
         tap = false;
       });
       document.addEventListener('touchend',function(e) {
          if(tap) {
             //users tapped the screen
          }
       });
    </script>
    Something along the lines of that.

  3. #3
    Join Date
    Feb 2005
    Location
    Norwich, UK
    Posts
    138
    Thanks, that seems to get me some of the way there. I'm guessing to register a 'double click' I'll need to do it manually - set a timer on the first touch, and if a second touch happens within a set time, it's a double click?

    And how would I get the coordinates of the touch? I'm using
    Code:
    evt = window.event;	
    targX = evt.clientX;
    	targY = evt.clientY;
    for the mouse click.

    Is there something similar, eg
    Code:
    e.clientX;
    ?

    Cheers for the help - bit of a JS newbie...
    _____________________________________________

    http://www.djillustration.com

  4. #4
    Join Date
    Dec 2012
    Posts
    97
    You can see where the user tapped the page using the changedTouches object

    Code:
    <script>
       var tap = true;
       document.addEventListener('touchstart',function(e) {
          tap = true;
       });
       document.addEventListener('touchmove',function(e) {
         tap = false;
       });
       document.addEventListener('touchend',function(e) {
          if(tap) {
             var touch = e.changedTouches[0];
             var pageX = touch.pageX;
             var pageY = touch.pageY;
          }
       });
    </script>

  5. #5
    Join Date
    Feb 2005
    Location
    Norwich, UK
    Posts
    138
    Superb, thanks very much!
    _____________________________________________

    http://www.djillustration.com

  6. #6
    Join Date
    Dec 2012
    Location
    NSW Australia
    Posts
    73
    I recently developed a simple game for a web site intended for desktop browsers only using click events for interaction..

    Feedback received was that both iOS and Android phones worked without any problems as the OS converted the click events to TOUCH events.

    I did not use the onClick style to register an event but the click event registered with the addEventListener.

    The conversion was unexpected so kudos to the mobile developers in both camps

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles