Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mobile Safari Event Issue

I have designed a website with a menu that is initially invisible. When the user clicks on a button, the menu becomes visible. There are two ways for the user to hide the now visible menu:

  1. Click the button that caused the menu to become visible
  2. Click anywhere on the web page that isn't the menu

The way I have coded the second option is to tie an onclick event to the window element, and have it compare where the user clicked to the menu's position to determine if the menu should be hidden. This works great in Firefox and Safari, but it fails in Mobile Safari.

I noticed that the window onclick event only fires when I click on another element with an onclick event already assigned. If I click on an element with no event(s) assigned, the window's onclick event never fires. If I click on the button which displays the menu, it fires along with the event tied to the button.

Is it possible to assign events to the window element in Mobile Safari?

like image 566
mouth Avatar asked Jul 13 '10 16:07

mouth


3 Answers

I'v been encountering this same problem. Here is what worked for me. (Note: I am working within a Modernizr and jQuery context)

First, I add a custom Modernizr class using Modernizr's addTest Plugin API to test for iOS, which will add the class appleios or no-appleios accordingly.

Because in my research the body seems to fire events on it's own agenda, I am taking a little precaution by wrapping all the document's content with an element in an iOS context. Then I add an event handler to this element.

$(".appleios body").wrapInner('<div id="appleios-helper" />');
$("#appleios-helper").bind("mouseup", function(){return;});

What was suggested earlier in this thread is using void(0). I did some quick testing, and found that void(0) as the event just wasn't causing touches on the body to be recognized. When I plugged in my own "empty" function in the form of function(){return;} things started working.

This all hinges on the fact that no events are fired in Mobile Safari unless the element explicitly has events to fire (Safari Web Content Guide.) By inserting this empty event on the wrapper, things will bubble up to the body.

If you're doing strait JavaScript with none of these libraries, the same effect could be achieved in the HTML markup

<html>
...
<body>
<div id="appleios-helper" onmouseup="function(){return;}">
...
</div>
</body>
</html>

This worked for me to hide tooltips when touching anywhere on the document's body. Your mileage may vary.

like image 132
danott Avatar answered Nov 01 '22 14:11

danott


Simply adding the dummy onclick handler to the html body works for me:

<body onclick="void(0)">

Note that I am using usual live event handlers as shown below:

function liveHandler( event ) {
    var target = event.target; ...}

window.addEventListener(evtype, liveHandler, true);
// evtype such as 'mousedown' or 'click'
// we use the capturing mode here (third parameter true)
like image 29
Louis LC Avatar answered Nov 01 '22 15:11

Louis LC


This is an old question, but I struggled with the same thing today.

I found that using touchstart event works.

I solved it like this:

var isTouchDevice = 'ontouchstart' in document.documentElement;
if (isTouchDevice) {
  // Do touch related stuff
  $(document).on('touchstart', function (event) {
    // Do stuff
  });
} else {
  // Do non-touch related stuff
  $(document).on('click', function () {
    // Do stuff
  });
}
like image 1
Lasse Skindstad Ebert Avatar answered Nov 01 '22 16:11

Lasse Skindstad Ebert