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

Hallvord R. M. Steenhallvors Friday, October 17, 2008 1:16:38 PM

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 Friday, October 17, 2008 1:46:47 PM

navigator.appVersion

Daned4n3 Friday, October 17, 2008 2:02:14 PM

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

Kirilljarinkirill Friday, October 17, 2008 2:03:51 PM

navigator.userAgent is shorter

Daned4n3 Friday, October 17, 2008 2:11:06 PM

navigator.appName

Kirilljarinkirill Friday, October 17, 2008 2:16:15 PM

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

Francisco iopossum Friday, October 17, 2008 3:02:45 PM

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)

Francisco iopossum Friday, October 17, 2008 3:20:35 PM

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?).

Mathias Plichtawrtlprnft Friday, October 17, 2008 4:05:19 PM

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

Michael A. Puls IIburnout426 Friday, October 17, 2008 4:54:14 PM

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

Michael A. Puls IIburnout426 Friday, October 17, 2008 5:09:14 PM

<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 Friday, October 17, 2008 7:46:13 PM

alert(frames);

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

temp1abc Friday, October 17, 2008 11:10:01 PM

alert(top)

(same result as alert(frames))

qwo Saturday, October 18, 2008 6:46:52 AM

2temp1abc

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

Hallvord R. M. Steenhallvors Saturday, October 18, 2008 8:23:33 PM

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_ Sunday, October 19, 2008 11:28:51 AM

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.

Hallvord R. M. Steenhallvors Sunday, October 19, 2008 10:14:57 PM

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

arantius Monday, October 20, 2008 3:10:24 AM

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 Monday, October 20, 2008 12:44:43 PM

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 Tuesday, October 21, 2008 1:56:27 AM

//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 Wednesday, October 22, 2008 5:20:38 AM

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'

Hallvord R. M. Steenhallvors Wednesday, October 22, 2008 2:54:25 PM

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

Jimtoyotabedzrock Wednesday, December 17, 2008 8:09:04 AM

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.

Hallvord R. M. Steenhallvors Wednesday, December 17, 2008 6:26:27 PM

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 Sunday, March 8, 2009 9:46:23 AM

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)

Anonymous Friday, April 22, 2011 8:58:18 AM

Анонимно writes: Have no enough money to buy a house? You not have to worry, because it's achievable to receive the loan to resolve such kind of problems. Thus get a bank loan to buy everything you want.

Write a comment

You must be logged in to write a comment. If you're not a registered member, please sign up.