iPhone SVG Events
The way the events work on a browser is pretty simple. If you want to listen to a particular event (say mousedown) on the whole document, you simply add an event listener to the document: document.addEventListener("mousedown", listenerFunction, false);. Setting 'false' for the 'useCapture' argument sets the event to bubble, which is the preferred event flow.
The test that we have is really simple. On your iPhone, the page renders a circle, a rectangle & of course these are rendered within a SVG container which is created within a background div. Tap on the circle, then rect and finally the background div. The test pages have 'touchstart' or 'click' event listeners registered by default. Click the 'Add'/'Remove' buttons to add/remove event listeners from the SVG element. There are 2 listener functions, one for logging document events and one for svg events. The events are logged with 'short function name: event.type : event.target'.
If you didn't know this already, you can enable the debug console in Safari on the iPhone.
Now, on the iPhone, the 'touchstart' event is similar to the 'mousedown' on a browser. Go to http://ecodevil.net/dev/blog/events_touch.html using your iPhone. Click on the circle, then rect and finally the white background. You should see 3 events logged.
Now click on the 'Add' button and as expected you will see 1 event (for the button). Now clear the console and click the circle, rect & background again. This time, you will have 5 events logged.
This is actually the expected behavior.
Now from your desktop Safari, go to http://ecodevil.net/dev/blog/events_click.html. You can enable your developer menu as well, see 'Safari Developer FAQ'. You will notice that you have 3 click events logged.
Now click on the 'Add' button and try the test again. You will once again have 1 + 5 events.
Again try http://ecodevil.net/dev/blog/events_click.html using your iPhone 2.1. When you run through the first set of tests, you will see no events logged.
Now click on the 'Add' button and try the test again. You will now see 4 events logged, but there is no click event on the background div.
So What?
So basically, events don't work exactly the same on the iPhone as the desktop browser. On a desktop browser, if an event listener is registered to listen to a particular event on the document, any event of the same type, on any element on the document bubbles up and will be logged, unless the event is stopped from propagating. But seems like on the iPhone, the touch/gesture events work by default on the document as well as the SVG element. But for the click/mouse* events to work on the document, there needs to be an event listener registered to listen to the SVG element. And removing this event listener will stop all click/mouse* events on the document.
The test that we have is really simple. On your iPhone, the page renders a circle, a rectangle & of course these are rendered within a SVG container which is created within a background div. Tap on the circle, then rect and finally the background div. The test pages have 'touchstart' or 'click' event listeners registered by default. Click the 'Add'/'Remove' buttons to add/remove event listeners from the SVG element. There are 2 listener functions, one for logging document events and one for svg events. The events are logged with 'short function name: event.type : event.target'.
If you didn't know this already, you can enable the debug console in Safari on the iPhone.
Now, on the iPhone, the 'touchstart' event is similar to the 'mousedown' on a browser. Go to http://ecodevil.net/dev/blog/events_touch.html using your iPhone. Click on the circle, then rect and finally the white background. You should see 3 events logged.
Now click on the 'Add' button and as expected you will see 1 event (for the button). Now clear the console and click the circle, rect & background again. This time, you will have 5 events logged.
This is actually the expected behavior.
Now from your desktop Safari, go to http://ecodevil.net/dev/blog/events_click.html. You can enable your developer menu as well, see 'Safari Developer FAQ'. You will notice that you have 3 click events logged.
Now click on the 'Add' button and try the test again. You will once again have 1 + 5 events.
Again try http://ecodevil.net/dev/blog/events_click.html using your iPhone 2.1. When you run through the first set of tests, you will see no events logged.
Now click on the 'Add' button and try the test again. You will now see 4 events logged, but there is no click event on the background div.
So What?
So basically, events don't work exactly the same on the iPhone as the desktop browser. On a desktop browser, if an event listener is registered to listen to a particular event on the document, any event of the same type, on any element on the document bubbles up and will be logged, unless the event is stopped from propagating. But seems like on the iPhone, the touch/gesture events work by default on the document as well as the SVG element. But for the click/mouse* events to work on the document, there needs to be an event listener registered to listen to the SVG element. And removing this event listener will stop all click/mouse* events on the document.
Labels: iPhone, JavaScript, SVG