开发者

How is it that Javascript can function differently from browser to browser?

开发者 https://www.devze.com 2023-03-31 13:43 出处:网络
I don\'t think I need to provide examples because every web developer knows that you need to test your Javascript to make sure it\'s compatible across different browsers.What I don\'t get is why.Isn\'

I don't think I need to provide examples because every web developer knows that you need to test your Javascript to make sure it's compatible across different browsers. What I don't get is why. Isn't Javascript Javascript? It'd be like the .NET framework behaving differently on one computer than it does on another. Can anyone shed some light on this, possibly with some source links to go along with 开发者_开发技巧their answer?


The short answer is every browser writes it's own interpreter for JS

The long answer

To understand why a programming language functions differently you have to understand what happens to a programming language at execution. First there are two types of languages interpreted and compiled. Compiled languages are turned from people readable "code" to machine code before execution and distributed as a binary.

Interpreted languages are compiled on the fly, JS is one of those languages. What does it mean to compile a language on the fly? Well, a computer can understand nothing but 1's and 0's and because of this all higher level languages must be broken down to instructions and then binary. When something is compiled on the fly it compiles a line, then runs it, then compiles the next. (of course there are intricacies but that really is the short of it)

Because JS really has never had a stand alone interpreter until recently, and the browser is JS's main purpose every company had to write their own interpreter that would go into their browser. Microsoft, Mozilla, Netscape... Everyone needed to think of how certain things could be handled and then execute them.

Because of this two things happen first, you are never gonna get the same thing if two people are working on it. Think about when you take an intro to Comp Sci course everyone has to make a calculator app but everyone takes a different approach. That happens all the time with js, and causes some browsers to crawl while others fly.

The second is that companies get set in their ways, they have too much time and energy invested in their interpreter and don't want to start over when modifying works and is cheaper. This means that differences that arose in the past when JS was less used and no where near as critical to web development will remain just because it's really difficult to roll out a whole new version of IE with webkit when it has been centered around it's own interpreter for years.


Read this -> http://www.quirksmode.org/js/intro.html there are lots of version of the JavaScript implementation - all of them have differences.


Each browser has a different version of Javascript, and some implement only certain features of each version. Here are the releases notes for IE9, that state javascript performs differently on IE8 and IE9.

Here is a list of versions.


usually, there is not different of javascript language between different browser, the problem is that DOM, HTML and event are different in different browsers. some javascript library can help reduce gap, ie: jQuery (only reduce gap)


Well, javascript is in fact a subset of EcmaScript which is a standard recommendation. The implementation of this recommandation among the browsers depends only of the willingness of the editors. This leads to several implementation (javascript, jscript, v8, etc...) all behaving differently in certain cases. Add to this that the layout engine differs from browser to browser (gecko, trident, webkit, etc...) and you'll see that it's not that simple to use javascript ;) .


Well, as a matter of fact, .NET does behave differently, depending on which implementation (Microsoft's or Mono) you use.

That said, it is the same with Javascript, with the small exception, that the standard is a moving target. It was introduced back in the days by Netscape, copied partly by Microsoft as JScript, standardized partly by ECMA and extended independently by the browser vendors, each one with their own idea of what would be a good idea to have in Javascript.

It's hard to define the Javascript. What is usually implemented as a baseline in all nowadays browsers is the feature set known as Javascript 1.5 aka ECMA-262 3rd edition. The browser vendors (and others) work on something called ECMAScript 5, but it will last years, until all browsers support this fully.

And then of course, every browser has its own bugs in its implementation. One of the most (in-)famous examples is the trailing comma thing in IE:

// works in all browsers:
var a = [1, 2, 3]
// works in all browsers but IE
var b = [1, 2, 3,]


Internet Explorer has JScript, this is Microsoft own JavaScript implementation. They have a long story of not following any specifications.

As for the others - they make their own implementations of the functions and objects in JavaScript. For example you have a theoretical function foo, and it is the same name on every browser. But the way it is written is different. Which may cause difference in the time for the execution or something else.

One other thing - every browser has its own specific functions. Nobody can force them not to have such.

0

精彩评论

暂无评论...
验证码 换一张
取 消