Skip navigation.

miscoded

the web is a hack

shortest incompatible script challenge

, ,

We all know that the interoperability situation on the Web has been abysmal. Web developers everywhere are vocally voicing their complaints of wasted time and money due to browser differences. Standards bodies that are meant to solve the interoperability problem become battle grounds of special interests, or create ivoryish spec-monsters that end up fragmenting the web even more. The dazzling bells and whistles of plugins threaten open standards..

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.. p: So the grand price is a souvenir 1000 krónar bill from Jon's country of origin.)

Ladies and gentlemen, post your answers in the comments.

My O statuses..and the winners are..

Comments

hallvors 17. October 2008, 13:16

Before anyone asks, there are no rules. For example, you don't need to limit it to pure ECMAScript - the solution can involve the DOM, host objects, markup, CSS, anything. I suppose the code will be shorter if you don't but anything goes :smile:

robbertbroersma 17. October 2008, 13:46

navigator.appVersion

d4n3 17. October 2008, 14:02

how about this:

alert(navigator.userAgent)

it does produce a different result everywhere, but it's a little too obvious P:

EDIT: robbertbroersma beat me to the punch :smile:

jarinkirill 17. October 2008, 14:03

navigator.userAgent is shorter

d4n3 17. October 2008, 14:11

navigator.appName

jarinkirill 17. October 2008, 14:16

2 d4n3: navigator.appName is 'Netscape' in latest FF and Safari

iopossum 17. October 2008, 15:02

try{x}catch(e){alert(e)}

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

Event better:

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

for(;;)

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

Not as short, but...

(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

<param>
<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

alert(frames);

// Opera: "[object WindowCollection]";
// Firefox: "[object Window]";
// Safari: "[object DOMWindow]";
// IE: "[object]";

temp1abc 17. October 2008, 23:10

alert(top)

(same result as alert(frames))

qwo 18. October 2008, 06:46

2temp1abc

alert(top) returns "[object Window]" in Opera and Firefox

hallvors 18. October 2008, 20:23

navigator.appVersion



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

prompt(alert())


- an example so basic it's trivial.. :smile:

Though it's long, I'll post this entry for its "huh?" value:

<body onload="alert(typeof this.onload+typeof document.onload+typeof document.body.onload)">



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

hmm. I think the entries that rely on toString methods outputting different things for errors/dom objects are not within the spirit of the challenge. I do not think this constitutes a compatibility problem. I.e. nothing could possibly break because of that.

hallvors 19. October 2008, 22:14

(I agree with that - but I said there were no rules. :-p)

arantius 20. October 2008, 03:10

setTimeout(alert,0)

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

For different *numeric* results in every browser, try this:

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. :smile:

garethheyes 21. October 2008, 01:56

//a in FF
//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 P:
Nice blog though I read it all the time :wink:

drfe 22. October 2008, 05:20

x()

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:

alert(/a/[-1])


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 :smile:. If spam becomes a nuisance I'll have to change it again because I do NOT have time for comment moderation, unfortunately..

Originally posted by drfe:

x()



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 :wink:. I admit to unclear requirements in the original post p:

toyotabedzrock 17. December 2008, 08:09

How do plugins threaten open standards? Most plugins for Firefox add to the browsers ui and don't actually touch the content. When they do its usually via a javascript, yet it has the ability to store local info and provide a nice ui to control it.

hallvors 17. December 2008, 18:26

Firefox "extensions" are a different type of plugin. "Plugin" in this context means something like Adobe's Flash and Acrobat Reader plugins, Microsoft's Silverlight and Windows Media Player and Apple's QuickTime. Their document- and application-formats sometimes compete with the HTML+JS+CSS set of technologies (e.g. when information is published in PDF format instead of HTML, when web applications are written in Flash instead of in HTML+JS..), and those who believe in the importance of open standards will claim this leads us to a more fragmented and less interoperable web.

(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

Anonymous writes:

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)

How to use Quote function:

  1. Select some text
  2. Click on the Quote link

Write a comment

Comment
(BBcode and HTML is turned off for anonymous user comments.)

If you can't read the words, press the small reload icon.


Smilies