Safari on the iPhone: 2 Steps Forward, 1 Step Back

By on August 2, 2007 10:03 pm

I’ve been following John Gruber on Twitter, who just noticed that the iPhone update (1.0.1) now updates Safari with the ability to send key events. Fantastic! 2 steps forward.

But, onkeyup is broken. You see, the onkeyup event is supposed to be called after a key event has officially propagated through the page. This is very important, because by the time onkeyup gets called, the page should have adjusted itself for this newly inserted character. For example, in a text input box, when you press a key it doesn’t show up in that input box until after onkeydown and onkeypress have been called, but does show up before onkeyup is called. Pretty much the whole reason that onkeyup hook exists at all, is so that you can be notified after this change has happened. The iPhone sends onkeyup before the page has been updated. 1 step back.

What this means is that sites that rely on using the onkeyup event to tell what is in their text entry box will break. There is a simple solution though! (other than having to find the key value in the event object) Just use setTimeout, with a timeout of 0 and you’ll be able to see the proper value. For example:

  input.onkeyup = function(e){
    setTimeout(function(){ alert(e.target.value); }, 0);
  }

Comments

  • Pingback: BarelyBlogging » Blog Archive » links for 2007-08-04()

  • Joel RF

    To me it looks like the onsubmit event was modified, now none of my AJAX.NET applications work. Instead of AJAX style partial postbacks you get normal full postbacks, this didn’t happen with the previous firmware.

    This doesn’t only affect the postback in AJAX.NET, if you used the onsubmit event to validate form fields before submit this is broken too. I’m very disappointed.

  • Thanks Joel, I’ll run some tests on that as well and verify just how broken it is.

    Also worth mentioning is that the onkeyup even doesn’t get fired if preventDefault is called on the event object in either onkeydown or onkeypress. Yet another idiosyncrasy to worry about.