JavaScript and HTML: Forgiveness by Default

I've been troubleshooting a bit of JavaScript lately, so I've enabled script debugging in IE7. Whenever the browser encounters a JavaScript error on a web page, instead of the default, unobtrusive little status bar notification..


This is a companion discussion topic for the original blog entry at: http://www.codinghorror.com/blog/2007/04/javascript-and-html-forgiveness-by-default.html

Hows the old saying go? “Be conservative in what you do, be liberal in what you accept from others”? I think that applies here as well as it does to any programming.

I recommend reading all three of Mark Pilgrim’s posts on this topic, because it will inform your error handling strategies in your own applications:

http://diveintomark.org/archives/2004/01/14/thought_experiment
http://diveintomark.org/archives/2004/01/16/draconianism
http://diveintomark.org/archives/2004/01/19/validator-services

I’ve never felt the compiler was flagellating me, except when I had to take over someone else’s code and discovered that they’d never heard of warnings…

It’s my opinion that if in the beginning of the web HTML standards were as good as XHTML, and clients were mandated to display high-quality, informative error messages on malformed content, then we’d be in a lot better state today. Graphical browsers would all render HTML the same, execute Javascript as expected, etc. Even “Joe User” would benefit as the tools he’d use to output HTML would output with standards compliance as a requirement.

Conversely, programming for the WWW would actually be a pleasure. Instead of shotgun programming, tweaking this HTML, hacking that CSS, we’d actually get work done at a reasonable pace.

To say that the web needs to be forgiving of errors simply because it’s widely adopted neglects that it started off on the wrong foot in the first place.

it’s a deadlock, any ‘strict’ web browser will never become popular because they are unuseable in the real world. But the popularity of a tolorent browser will only encourage bad coding.

mpan3, non-web programmers have to adhere to strict language syntax and catch any possible errors why shouldn’t web developers? Personally, I check to make sure my work is strict XHTML, CSS and doesn’t produce JS errors. I test it all in multiple browsers. I don’t see why web developers should have it easy…

It’s a double edged sword as no two “workarounds” behave the same way. It makes designing an HTML page to look the same on all browsers a real nightmare.

Of course, even if all the browsers were strict, there is still room for interpretation in any spec. But at least the differences would be minimal. At least I would hope.

There is a great money-making opportunity here for the web site validation people.

Gosh. It’s like millions of people have a vested interest in teh Intarwebs being user friendly, or something.

Using variable i in that loop without var keyword makes it to refer to global variable i within the script and not local one to the loops scope. Most likely a programmer error…

Dave’s evaluation of what is going on in the Javascript is incorrect. “i” is not initialized to startIndex at the beginning of each iteration of the loop in Firefox:

/* prints 0 to 999 in both IE and Firefox */
var endIndex=1000;
var startIndex=0;

for(i=startIndex; i endIndex; i++) {
document.write(i + ‘br’);
//Do some stuff here
}

My guess is that your “//Do some stuff here” manipulated (or called functions which manipulated) a variable “i” as well, and in Javascript, if you use a variable without declaring it with “var”, the scope is global (otherwise it is local to the containing function). Why this does not affect Internet Explorer is anyone’s guess. Without seeing the remainder of the source code, I’d speculate that there is separate code to support IE and Firefox and only the Firefox code uses another global instance of “i”.

However, as the sample code above demonstrates (tested in Firefox 2.0.0.3 and IE 7), the loop completes normally. There is no bug, error or differences between the way IE and Firefox handle the scoping rules and loop.

The comments completely broke my code. Hopefully this works:

/* prints 0 to 999 in both IE and Firefox */
var endIndex=1000;
var startIndex=0;

for(i=startIndex; i endIndex; i++) {
document.write(i + ‘br’);
//Do some stuff here
}

Brendan: you might want to take a look at the WHAT-WG’s “HTML 5” work (which has now been proposed to the W3C’s HTML working group as a starting point for new work); one of the major design goals of the specification is to set out unambiguous rules for handling malformed HTML.

Doesn’t help with JavaScript (and Grant is almost certainly right in diagnosing global scope as the root of Dave Murdock’s loop problems, in which case Firefox is behaving correctly), but at least it’s a start.

Javascript is the single reason I’m looking to move away from web programming as a career. It is the cause of 90% of my testing and debugging. I want to debug logic, not fiddly cross browser issues.

Javascript is the single reason I’m looking to move away from web
programming as a career. It is the cause of 90% of my testing and
debugging. I want to debug logic, not fiddly cross browser issues.

Use Flex. That gives you flash apps that behave the same everywhere. The language of Flex is ActionScript – JavaScript with decent static typing. It’s really amazing how that “simple” addition fixes JavaScript’s stupidities.

JavaScript errors are so pervasive, in fact, that it’s easy to understand why IE demotes them to nearly invisible statusbar elements.

Everyone does, actually, and MSIE is the only browser who switches to modal dialog when you allow it to launch a debugger.

Oh, and it’s the only browser whose error messages completely and utterly sucks.

Warnings and errors are good for developers. That kind of environment for JS is harmful to the people writing JS code.

Bad developers don’t care (when they have a strict compiler, they change the code to make the code compile, they don’t try to understand why they have errors and make their code better), good developers in JS have LINT, and Firefox’ Strict mode, and Firebug (of course these are not enough because – business as usual – IE still has retarded behaviours, but that makes for a much stricter Javascript environment)

“We scratch our heads and wonder why the browser can’t give us the punishment we so richly deserve for our terrible, terrible mistakes”

Priceless :))

“Use Flex. That gives you flash apps that behave the same everywhere.”

Bzzzt! That answer is incorrect. Those flash apps fail to work on my Linux/PPC system, as Adobe refuse to release a player for that system.

The whole basis for the success and popularity of most of the web is due to its reliance on open standards that are implemented by multiple vendors for any platform you care to name. Flash cannot work for a fair proportion of computing devices because of this. Take mobile phones. Many of them now run Linux run on non-x86 chips and have full network stacks and are capable of browsing the web, but flash players simply will not work for them.

The majority of the web is client/platform independent. Lets keep it that way, please? Lets not go back to the dark ages where vendors locked you into proprietary file formats. We’re only just starting to get out of that trap with document formats and ODF.

Having things behave the same everywhere (given a sufficiently loose definition of “everywhere”) because only one vendor is capable of supplying the software required to view the content is not a step up from HTML/JS.

Logical/runtime errors are handled about the same as any other language.

True, but JavaScript is also a dynamic language, so you don’t have the same compile-time warnings as you do in statically-typed languages… or dynamic languages with static declaration options.