I yesterday faced a very peculiar situation. A component from one of my web apps started throwing javascript errors on my iPad Safari as I was trying out some things from the app. It was a bit surprising as I had successfully tested the same component at the beginning of this week on the same iPad without any issues, no changes had been made to the component and nothing else struck me when I faced the issue. The fact that the same component was running successfully on all versions of all major browsers on a PC confused me further.

I was not on my primary dev machine but the issue needed to be resolved as there was an update rollout pending over the weekend. As I resorted to the ancilliary technique of placing “alert” messages at various portions of the code, I found something totally unexpected (which I consider a bug with iPad Safari). Consider the following simple lines of javascript code:

{syntaxhighlighter brush: jscript;fontsize: 100; first-line: 1; }function test(arguments){
alert(arguments);
alert(eval(‘arguments’));
}

test(‘Hello’);{/syntaxhighlighter}

If you put this javascript code on a web-page, and execute it, what do you expect. Well, no prizes for guessing: 2 alert message boxes in succession both with the message “Hello”, right. I also expected the same, and in fact, it was happening like this a week earlier in my iPad 2, but not yesterday. You can try this in the live example below, try clicking both the buttons on any browser and then in iOS 5 Safari in iPad, a (rather unpleasant) surprise awaits you:

You will see the expected result in all browsers except in iOS 5 Safari, where the second statement of the method (alert(eval(‘arguments’))) shows up a [object Arguments] message. Can you guess where Safari has gone wrong.

You might have figured it out already, in the eval call, Safari is not using the “arguments” variable from the function’s parameter list, rather it’s using a reference to the implicit “arguments” variable that is present in each javascript function. Clearly this is a bug with iOS 5 Safari.

To put it more precisely, a parameter named “arguments” used inside eval in iOS 5 Safari refers to the function’s native “arguments” object rather than the explicit parameter with the same name.

This problem does not occur if you use a variable with the name “arguments” as a local variable inside a function and then eval it, the problem only occurs when a variable with such a name is present as a parameter to the function and you eval it.

And now guess where things went wrong for me. I upgraded my iPad to iOS 5 somewhere between this week.

The solution well is simple, rename your parameter. In my case, I changed the parameter name to “args” and things returned back to normal.