views:

192

answers:

7

I'm not a JavaScript wiz, but is it possible to create a single embeddable JavaScript file that makes all browsers standards compliant? Like a collection of all known JavaScript hacks that force each browser to interpret the code properly?

For example, IE6 does not recognize the :hover pseudo-class in CSS for anything except links, but there exists a JavaScript file that finds all references to :hover and applies a hack that forces IE6 to do it right, allowing me to use the hover command as I should.

There is an unbelievable amount of time (and thus money) that every webmaster has to spend on learning all these hacks. Imagine if there was an open source project where all one has to do is add one line to the header embedding the code and then they'd be free to code their site per accepted web standards (XHTML Strict, CSS3).

Plus, this would give an incentive for web browsers to follow the standards or be stuck with a slower browser due to all the JavaScript code being executed.

So, is this possible?

+3  A: 

Check out jQuery it does a good job of standardizing browser javascript

Along those same lines explorercanvas brings support for the HTML5 canvas tag to IE browsers.

Gavin Miller
+1  A: 

You can't get full standards compliance, but you can use a framework that smooths over some of the worst breaches. You can also use something called a reset style sheet.

Joel Coehoorn
A: 

Actually you can,there are lots of libraries to handle this issue. From the start of the time, javascript compliance issue always was a problem for developers and thanks to innovative ones who developed libraries to get over this problem...

One of them and my favorite is JQuery.

Braveyard
+1  A: 

There's a library for IE to make it act more like a standards-compliant browser: Dean Edwards' IE7.

ceejayoz
IE7-js was named back in the days when IE6 was the latest available version, and Microsoft had no plans to update it. Now that IE8 is out, the name is confusing.
Kevin Panko
Yeah. He posted a blog post about potentially updating the name.
ceejayoz
This? http://dean.edwards.name/weblog/2005/02/ie8/
Kevin Panko
+1  A: 

Like a collection of all known javascript hacks that force each browser to interpret the code properly

You have two choices: read browser compatibility tables and learn each exception a browser has and create one yourself, or use avaiable libraries.

If you want a javascript correction abstraction, you can use jQuery.

If you want a css correction abstraction, you can check /IE7/.

I usually don't like to use css corrections made by javascript. It's another complexity to my code, another library that can insert bugs to already bugged browsers. I prefer creating conditional statements to ie6, ie7 and such and create separate stylesheets for each of them. This approach works and doesn't generate a lot of overhead.

EDIT: (I know that we have problems in other browsers, but since IE is the major browser out there and usually we need really strange hacks to make it work, css conditional statements is a good approach IMO).

GmonC
+4  A: 

Plus, this would give an incentive for web browsers to follow the standards or be stuck with a slower browser due to all the JavaScript code being executed.

Well... That's kind of the issue. Not every incompatibility can be smoothed out using JS tricks, and others will become too slow to be usable, or retain subtle incompatibilities. A classic example are the many scripts to fake support for translucency in PNG files on IE6: they worked for simple situations, but fell apart or became prohibitively slow for pages that used such images creatively and extensively.

There's no free lunch.

Others have pointed out specific situations where you can use a script to fake features that aren't supported, or a library to abstract away differences. My advice is to approach this problem piecemeal: write your code for a decent browser, restricting yourself as much as possible to the common set of supported functionality for critical features. Then bring in the hacks to patch up the browsers that fail, allowing yourself to drop functionality or degrade gracefully when possible on older / lesser browsers.

Don't expect it to be too easy. If it was that simple, you wouldn't be getting paid for it... ;-)

Shog9
A: 

Before JavaScript 1.4 there was no global arguments Array, and it is impossible to implement the arguments array yourself without a highly advanced source filter. This means it is going to be impossible for the language to maintain backwards-compatibility with Netscape 4.0 and Internet Explorer 4.0. So right out I can say that no, you cannot make all browser standards compliant.

Post-netscape, you can implement nearly all of the features in the core of the language in JavaScript itself. For example, I coded all methods of the Array object in 100% JavaScript code.

http://openjsan.org/doc/j/jh/jhuni/StandardLibrary/1.81/index.html

You can see my implementation of Array here if you go to the link and then go down to Array and then "source."


What most of you are probably referring to is implementing the DOM objects yourself, which is much more problematic. Using VML you can implement the Canvas tag across all the modern browsers, however, you will get a buggy/barely-working performance in Internet Explorer because VML is markup which is not a good format for implementing the Canvas tag...

http://code.google.com/p/explorercanvas/

Flash/Silverlight: Using either of these you can implement the Canvas tag and it will work quite well, you can also implement sound. However, if the user doesn't have any browser plugins there is nothing you can do.

http://www.schillmania.com/projects/soundmanager2/

DOM Abstractions: On the issue of the DOM, you can abstract away from the DOM by implementing your own Event object such as in the case of QEvent, or even implementing your own Node object like in the case of YAHOO.util.Element, however, these usually have some subtle changes to the standard API, so people are usually just abstracting away from the standard, and there is hundreds of cases of libraries that abstract away.

http://code.google.com/p/qevent/

This is probably the best answer to your question. It makes browsers as standards-compliant as possible.

http://dean.edwards.name/weblog/2007/03/yet-another/

jhuni