views:

200

answers:

4

Here's something I've been pondering after countless hours fixing JS to be cross-browser compatible (mostly IE): Why isn't Javascript consistent accross browsers?

I mean, why can't JS be nice like Java and Flash? Instead, we have to resort to frameworks like jQuery. Don't get me wrong, they make my life easier - but why do they even exist in the first place?

Is there a historical reason for this? Do companies rolling out browsers just ship their own JS engine? What are the politics that make standardization so difficult?

(Note: I understand that a good part of the problem is DOM related, but the question remains).

+7  A: 

Do companies rolling out browsers just ship their own JS engine?

Yup, that's probably the main reason. There is no unified JS engine; there are various implementations of ECMAScript.

Pekka
Just like browsers with rendering, there are different ways to interpret the specifications, different ways to implement them, and even worse: different ways to fill in the gaps specifications leave!
Bob Fincheimer
You can safely leave out ‘probably’. ;)
Marcel Korpel
+14  A: 

The Javascript core language for the most part is consistent ( Referring to ECMAScript version 3 released in 1999. )

It's the DOM implementations that cause headaches. Partly because at one point there was no DOM specification so browsers could do whatever the hell they wanted in terms of making up the rules for which to access and manipulate html elements in a web page.

For example:

  • window.addEventListener for DOM supporting browsers, while window.attachEvent for IE.
  • textContent for DOM supporting browsers, innerText for IE.
  • Memory leakage for attached event handlers in IE so you have to unload them manually
  • getElementById is buggy in IE and Opera because it returns elements by name
  • getAttribute('href') returns inconsistent values

There are also issues relating to the browser's CSS support.

  • IE6 doesn't support native PNGs so you are forced to use the filter library
  • Buggy animation in IE dealing with filter opacity

Language core inconsistencies would be things like

  • Inconsistencies between regex engines

But yeah, in short the point is that before, there was no standard. Since then, the w3 came up with standards, but every browser vendor has its own way of dealing with implementing it. There's no governing body that forces the vendors to fully apply the spec.

meder
+1 that's true and important to note. The language core *is* reliable. The DOM is where frameworks like jQuery come in (and do lots of good.)
Pekka
“The core language for the most part is consistent.” Not entirely: e.g., `Array.prototype.sort` isn't consistent in stability, the availability of functions like `Array.prototype.indexOf` and `Function.prototype.bind` vary widely, iterating over all properties of an object using `for (… in …)` isn't (or wasn't, I'm not sure) consistent, you'll need `hasOwnProperty`.
Marcel Korpel
`Array.prototype.indexOf` isn't a part of ECMAScript v3. Mozilla just threw it into Gecko in 2004/2005. IE6 was developed in 99-00 and released in 01. So how could they have implemented it if it wasnt in the language?
meder
`for..in` IS consistent. The `hasOwnProperty` IS consistent. If you extend `Object.prototype` it'll appear without using `hasOwnProperty` but the behaviour should be consistent across the board.
meder
`Function.prototype.bind` isn't defined in Firefox or Chrome, nor is it defined in IE. I think you're confusing user-defined functions which are naturally inconsistent and thinking they're built-in?
meder
Can you give me an example of `sort` inconsistency across browsers?
meder
You may be referring to ECMAScript version 5 built-ins with `Function.prototype.bind`, but how would it make sense to be complaining about a language version released in December 2009 when we're talking about browsers that have been out for YEARS? Opera, IE, Chrome V8, Gecko/SpiderMonkey/Rhino, .NET, Acrobat, iCab, KDE, Samba 4, KHTML all are ECMA-262, edition 3. ( And more.. ) So it would make sense to use ECMA-262 edition 3 as the standard for the "core". Not a version released last year.
meder
getAttribute is bugged under other situation in some version of IE. If you have a form with an element named "action" in it and you do form.getAttribute("action"), it will return the element in the form not the attribute action of the form in IE 6. I don't remember if the same behaviour occurs on other version too, but it might be there in IE 7 too.
HoLyVieR
Ok, you're right, I'm intermingling several things. Built-in `sort` is not stable in (at least) Chrome, or at least it was in September 2009. See http://stackoverflow.com/questions/1427608/fast-stable-sorting-algorithm-implementation-in-javascript.
Marcel Korpel
+2  A: 

Browsers roll their own implementation, plain and simple. It's the same reason why rendering and CSS and all that are different across browsers. Java/Flash/etc. are more universal because they're abstracted out of the browser and accessed via a plugin of some sort. But their actual core implementations are separate from the browser and controlled by a single vendor.

David
+2  A: 

To add to the other answers: there is a historical reason for this. I can write this myself, but quoting Wikipedia is easier on the fingers:

JavaScript was originally developed by Brendan Eich of Netscape under the name Mocha, which was later renamed to LiveScript, and finally to JavaScript. LiveScript was the official name for the language when it first shipped in beta releases of Netscape Navigator 2.0 in September 1995, but it was renamed JavaScript in a joint announcement with Sun Microsystems on December 4, 1995 when it was deployed in the Netscape browser version 2.0B3.

[…]

JavaScript very quickly gained widespread success as a client-side scripting language for web pages. As a consequence, Microsoft developed a compatible dialect of the language, naming it JScript to avoid trademark issues. JScript added new date methods to fix the non-Y2K-friendly methods in JavaScript, which were based on java.util.Date. JScript was included in Internet Explorer 3.0, released in August 1996. The dialects are perceived to be so similar that the terms "JavaScript" and "JScript" are often used interchangeably. Microsoft, however, notes dozens of ways in which JScript is not ECMA-compliant.

In November, 1996 Netscape announced that it had submitted JavaScript to Ecma International for consideration as an industry standard, and subsequent work resulted in the standardized version named ECMAScript.

As you can see, the standard, ECMAScript, was developed later than the original language. It's just a matter of adapting this standard in the current implementations of web browsers, that's still going on, as is the development of ECMAScript itself (e.g., see the specification of ECMAScript 5, published December 2009).

Marcel Korpel