JavaScript speeding ticket

As we know, timing is everything. No matter if we're talking choreography or computing, timing issues can mean big trouble. Here are two examples of code that depends on the timing and speed of the browser those scripts vere written for..

Says the famous Orkut if you try searching your friends:

<form method="POST" action="/Friends.aspx">
<input id="searchMyFriends"
                   onkeypress="if ((window.event ? event.keyCode : event.which) == 13) { handleDoSearch(); }"/>

(irrelevant HTML removed)
The handleDoSearch function does this:

function handleDoSearch() {
  var url = '/Friends.aspx';
  url += "?show=search&searchQuery=";
  url += encodeURI(document.getElementById('searchMyFriends').value);
  document.location = url;

When you type something into the "search my friends" box you are supposed to press enter to start the search. (I guess they don't worry about computer newbies who need a "Go" button even next to the URL field). Now, usually pressing enter in a text field should submit the form. When you press enter in this field, it actually sets off two actions: the browser prepares the form submit, and the JavaScript creates the URL and will set document.location. In this race, if the form submit wins, friends search doesn't work, if the JavaScript wins it does. In Opera, the form submit wins..

NRG is a big news site in Israel, and presents us with another interesting timing issue. Registered users have access to a "bookmark this article" feature that will add the article you're reading to your on-site bookmarks. In Opera, saving articles doesn't work even though you get a popup window with a message stating that article was successfully saved..

Here's how the script is meant to work:

  • script sets innerHTML of a SPAN to create an IFRAME that will load the address that does the actual work of adding the page to your "favourites".
  • it opens a popup (loading a separate address) to tell you that the page was successfully added (I presume – I don't read Hebrew but that's what the bug report says..)
  • it sets SPAN's innerHTML again, and this time to an empty string.

So we get into a race/timing condition: if innerHTML is reset to nothing too soon, the request to the address that actually adds the page to favourites won't happen. Seems Opera's is faster than the competition's?

..quite un-scientifically timed, adding two recordings of new Date().getTime() after the first and before the second .innerHTML set:

Opera: 125ms
IE: 1594ms

Right. I get this image of a webmaster in blue uniform with a police baton:
"Excuse me Sir, I believe your User Agent was caught speeding over there"

"Uh, was it?"

"Yes, it opened a popup window 12 times faster than the competition.
The fine is broken functionality in the article bookmarking feature."

My first JavaScript speeding ticket..

4 thoughts on “JavaScript speeding ticket

  1. (I guess they don't worry about computer newbies who need a "Go" button even next to the URL field).

    :left: Didn't Opera remove the Go button from the address bar? :whistle:

  2. Hi,Can something like that be fixed by an end-user (like, somehow telling Opera to slow down for certain sites) or only by the site's developers?Thanks

  3. Tombaz: the only practical way to fix it is User JavaScript (or browser.js – now it is patched 🙂 ). It would be somewhat stupid to create a new Opera setting named "slow down opening popup windows" because a single site had a script that depended on such behaviour.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s