shortest incompatible script challenge
Friday, 17. October 2008, 13:04:43
So, let's have some incompatibility fun.
Here's a challenge for JavaScript-skilled readers: Who can come up with the shortest possible JavaScript that produces 4 different results in the top 4 engines?
(For the purpose of this exercise let's define the top 4 browsers as IE, Firefox, Safari/WebKit and Opera - latest available final versions).
The winner gets fame and fortune. (Well, I think I can afford a reward of ISK1000 these days if anyone is interested - certainly our friends in Iceland really need someone wanting to buy their money..
Ladies and gentlemen, post your answers in the comments.










hallvors # 17. October 2008, 13:16
robbertbroersma # 17. October 2008, 13:46
d4n3 # 17. October 2008, 14:02
alert(navigator.userAgent)
it does produce a different result everywhere, but it's a little too obvious
EDIT: robbertbroersma beat me to the punch
jarinkirill # 17. October 2008, 14:03
d4n3 # 17. October 2008, 14:11
jarinkirill # 17. October 2008, 14:16
iopossum # 17. October 2008, 15:02
Safari: ReferenceError: Can't find variable: x
Firefox: ReferenceError: x is not defined
Opera: <localhost>
[Error:
name: ReferenceError
message: Statement on line 2: Undefined variable: x
Backtrace:
Line 2 of inline#1 script in file://localhost/Users/tolmasky/Desktop/a.html
try{x}catch(e){alert(e)}
]
IE: [object Error]
For reference:
try{x}catch(e){alert(e)} --> 24 chars
alert(navigator.userAgent) ---> 26 chars
alert(navigator.appName) ---> 24 chars (but fails to be different in safari and ff)
iopossum # 17. October 2008, 15:20
prompt(alert()) -->15 chars
Safari: alert that reads "undefined", hit OK -> confirm box that reads "undefined" with blank input field
Firefox: just doesn't run
Opera: *blank* alert box, hit OK -> confirm box that reads "undefined" with blank input field
IE: *blank* alert box, hit OK -> confirm box that reads "undefined" AND input field that contains the text "undefined"
Just doing alert(), prompt(), and confirm() produces one (different) pair of browsers in each case that are the same, same with alert(alert()) unfortunately (or fortunately?).
wrtlprnft # 17. October 2008, 16:05
Konqueror/Linux: A script on this page is causing KHTML to freeze. If it continues to run, other application may become less responsive.\n\n Do you want to abort the script?
Opera/Linux: Freezes the tab in some weird way, keyboard input is ignored, uses up one CPU core, other tabs and browser UI still work
Firefox/Linux: Nothing appears to happen
IE6/WinXP (German; sorry, I don't have anything else): Ein Skript auf dieser Seite verursacht eine Verzögerung in Internet Explorer. Falls das Skript weiter ausgeführt wird, verlängern sich die Antwortzeiten des Computers möglicherweise erheblich\n\nSoll das Skript abgebrochen werden? (I'm pretty sure it doesn't mention KHTML in the English version, so it's definitely a different response ;-) )
Or, alternatively, if that counts:
;
Opera/Linux: shows Opera/Linux UI
Konqueror/Linux: shows Konqueror/Linux UI
Firefox/Linux: Shows Firefox/Linux UI
IE6/WinXP: Shows IE6/WinXP UI
or even the empty script, same result :D
burnout426 # 17. October 2008, 16:54
(The first 2 are a stretch because 2 of the results for each appear to be the same to the user, but internally, they're different.)
Focus the input and press del
<input oninput="alert(1)">
Opera: Fires the event twice
Safari: Fires the event once
Firefox: Doesn't fire the event because the value didn't change
IE: Doesn't support the input event
Enter a char in the textarea:
<textarea oninput="alert(1)"></textarea>
Opera: Fires the event twice
Safari: input is broken on textareas
Firefox: Fires the event once
IE: Doesn't support the input event
<script>
var test = document.createElement("textarea");
test.value = "\n\r\n\r";
alert(encodeURIComponent(test.value));
</script>
Firefox: %0A%0D%0A%0D
Opera: %0D%0A%0D%0A%0D
Safari: %0A%0A%0A
IE: %0D%0A%0D%0A
burnout426 # 17. October 2008, 17:09
<script>alert(document.getElementsByTagName("param")[0]);</script>
Opera: object HTMLParamElement
IE 8 beta 2: object HTMLUnknownElement
FF 3: undefined (DOM isn't ready yet)
Safari: undefined (params not inisde <object> are dropped by the parser and *never* make it to the DOM)
qwo # 17. October 2008, 19:46
// Opera: "[object WindowCollection]";
// Firefox: "[object Window]";
// Safari: "[object DOMWindow]";
// IE: "[object]";
temp1abc # 17. October 2008, 23:10
(same result as alert(frames))
qwo # 18. October 2008, 06:46
alert(top) returns "[object Window]" in Opera and Firefox
hallvors # 18. October 2008, 20:23
LOL. This proves that it is possible to cheat even when there are no rules. Now since I get to be the judge I can probably cheat a bit too, make Opera mask as IE or Firefox and claim that two of the four engines do the same thing :-p
I quite liked
- an example so basic it's trivial..
Though it's long, I'll post this entry for its "huh?" value:
Opera: functionobjectfunction
IE (8, sorry): objectundefinedfunction
Safari: functionobjectobject
Firefox: functionundefinedundefined
How many years have we had to deal with BODY onload=""? Pre-HTML4 stuff, right? Not two browsers do the same thing..
More short'n'sweet peculiarities?
_Grey_ # 19. October 2008, 11:28
hallvors # 19. October 2008, 22:14
arantius # 20. October 2008, 03:10
19 bytes
IE6: no alert
FF3: semi-random small integer (see http://bit.ly/2cQMwB )
Opera 9.6: empty string
Safari 3.1: undefined
Less string cast trivialities, more real browser quirks.
pixycz # 20. October 2008, 12:44
var x=Date; alert(new x()/new x()/new x()/new x()/new x());
It has also nothing with the core/engine functionality, but it works, though.
garethheyes # 21. October 2008, 01:56
//does nothing in Safari
//returns undefined in Opera
//Dunno about IE but probably different
alert(/a/[-1])
btw it sucks you have to register to comment
Nice blog though I read it all the time
drfe # 22. October 2008, 05:20
Exception handling is a serious x-browser nightmare. This scratches the surface. Did I win?
FF:
message = x is not defined
fileName = http://www.plexode.com/cgi-bin/eval.py
lineNumber = 91
stack = calc()@http://www.plexode.com/cgi-bin/eval.py:91
calc()@http://www.plexode.com/cgi-bin/eval.py:91
once()@http://www.plexode.com/cgi-bin/eval.py:72
onclick([object MouseEvent])@http://www.plexode.com/cgi-bin/eval.py:1
name = ReferenceError
IE:
name = TypeError
message = Object Expected
number = -2146823281
description = Object expected
Safari:
message = Can't find variable: x
line = 1
sourceId = 39
name = ReferenceError
Opera:
message = Statement on line 1: Undefined variable: x
Backtrace:
Line 1 of eval script
x()
Line 37 of inline#1 script in http://www.plexode.com/cgi-bin/eval.py: In function calc
out.innerHTML = textToHtml(eval(expr).toString());
Line 18 of inline#1 script in http://www.plexode.com/cgi-bin/eval.py: In function once
calc();
Line 1 of function script
once()
...
opera#sourceloc = 1
stacktrace = n/a; see 'opera:config#UserPrefs|Exceptions Have Stacktrace'
hallvors # 22. October 2008, 14:54
Originally posted by garethheyes:
Nice one - though it doesn't qualify for the competition because IE also says "undefined".
I'm mystified by the Firefox response though. If it is a feature it looks like a very weird and quirky one, good catch!
And I changed my prefs to allow anonymous comments, thanks for pointing it out
Originally posted by drfe:
Good attempt but if you look at the other entries they all contain code to make some sort of visible output. If you include a try..catch and for..in on the error object's properties your entry isn't quite as short anymore
toyotabedzrock # 17. December 2008, 08:09
hallvors # 17. December 2008, 18:26
(To a certain extent those plugins are important because of the lack of good alternatives from the open standardisation processes - i.e. Flash video.)
Anonymous # 8. March 2009, 09:46
Wow, great post! Very interesting comments!
I always had problems with the regular expressions across browsers. Now the holes are mostly patched up but generally each would do basic operations slightly differently when you tried to use regular expressions in them.
Another thing I've had fun with is stylesheets. Try including some cross-domain javascript and then have that call document.write() to add a stylesheet. I know that you shouldn't do it but I have once or twice.
After that put in some style elements sing actual markup and specify completely different and contradictory rules. I can't remeber the details but I remember a situation where Webkit would ignore the javascrpt CSS, Gecko would let you use it but also let you override it, Trident would not let you use CSS to override the javascript-included stylesheet, even if you used !important (or maybe because of it, you know IE. I'm talking back in IE6 here) and Opera would error out and pretend that there was no DOM available to javascript, and throw up some weird debugging alert error message.
I can't provide example code because I've since learned what I was doing it wrong and how to do it properly (sorry).
...
I have had some great fun working across frames, too...
(And if you're cheating with markup and using specific browser versions, try using non w3c dom in applciation/xhtml+xml. Each browser shows a different level of support for the actual javascript and IE simply spits the whole page right out because it is too pathetic to even display a well-formed web page from 1999)