Skip navigation.

Dear Satan... I Mean SantaAll recent postsLetter Picker Controls: Your Take

Giving JavaScript Due Credit

Let’s do a virtual show of hands: how many of you, folks, don’t take JavaScript seriously? How many of you think there’s not much to it and it’s not worth the time actually learning it ’cause you can learn it by View Source alone?

JavaScript: The Definitive Guide Years ago I read one of those Teach Yourself JavaScript Really Quick or something books. Ever since I didn’t bother actually learning the ins and outs of JavaScript. I always thought of it as a low-grade interpreted language (I’m not a fan of interpreted languages in general) with weak syntax and lack of object-orientedness.

And then I saw a recommendation of JavaScript: The Definitive Guide by Konfabulator guys. I fully agree with them—it’s the best JavaScript resource hands down. I like it even more than the famed JavaScript Bible by Danny Goodman.

One of the shockers is that JavaScript is very much object-oriented! You write classes somewhat differently but you get used to it very fast.

If you do buy JavaScript: The Definitive Guide turn to section 12.2.1.1 on page 186. This section explains the language type attributes in a very detailed manner.

For example, right there the author points out:

The HTML 4 specification standardizes the <script> tag, but it deprecates the language attribute because there is no standard set of names of scripting languages. Instead, the specification prefers the use of a type attribute that specifies the scripting language as a MIME type.

I see plenty of code on the web (I do my share of View Source-ing) that has JavaScript defined the old way. The right way to declare a block of script is:

<script type="text/javascript"></script>

If you’re a big fan of this kind of declaration

<script language="JavaScript1.5"></script>

I don’t envy you because figuring out which browser supports what version of JavaScript is a mess.

By the way, which browser, do you think, is slacking the most in its support of the JavaScript spec? If you pointed your finger at Microsoft Internet Explorer for Windows you guessed it right. Even to this day IE 6 has glaring holes in its JavaScript implementation.

Anyway, another common misconception is that you can simply place a <script>...</script> declaration and the browser will figure out what to do with it. I’ve said this a number of times: stop abusing browsers! You add browser developers work hours to accommodate your laziness. The only person you’re cheating is yourself.

The last one is declaring variables with a var. Go to the previous paragraph and read the statement in bold again. Variable name resolution does not come for free. The interpreter has to walk all the way up the food chain and try to figure out where you declared something. Declare a variable with var to tell it where this variable belongs without second guessing. I’ve always been doing it due to my C++ background but now I understand why it is required.

This is not a JavaScript crib sheet in any way. If time allows, I’ll compile some JavaScript best practices guidelines one day.

Conclusion

I believe ASP.NET developers should harness the power of JavaScript better and do it properly! Don’t underestimate JavaScript—it’s very powerful, it’s just that most people neglect it. My advice is to stick to the standard and avoid proprietary hooks and "extensions" where possible. Maybe then we won’t have to put up with validator controls that work in only one browser and stupid stuff like that.

By saying all this I’m hoping to encourage my fellow ASP.NET developers to pay attention to this language. I encourage you to actually learn the nuts and bolts of it. There’s a lot more to it than what you see by snooping around somebody else’s page source.

Comments

Comment permalink 1 Jerome Bos |
Declaring variables with var isn't that simple. When you declare a variable inside a function the variable has "local scope" or "enclosing scope (when the variable is already declared outside the function)".
It's somehow the same as making your variables "private" or "public".
Although it might be a good book, I don't really think somebody can learn the ins and outs by reading a book.
Comment permalink 2 Milan Negovan |
Yep, variable declaration is trickier than it seems to be thanks to the scoping rules.

As to the book itself, I guess it depends on each person's background. If you know bits and pieces of JavaScript and need to fill in the gaps I think it's great.
Comment permalink 3 Mark Wubben |
Oh yeah, JavaScript rocks! As does the definitive guide, it's really quite good (and the reference is quite handy as well).

As for browser support, IE usually doesn't give me headaches. IE 5.0 misses on some features, because it uses an older JScript engine, but you can use JavaScript to work around that. One thing that IE is missing is "watch functionality", but it does have `onpropertychange` which sort of boils down to the same thing. IE does have a weird thing with object literals though, as does Opera... you can't have a comma behind the last property.

Talking about Opera, that is one weird browser. But, even more weird is Safari. For example it won't respect the constructor property when using your own classes.

As for DOM support, nothing can surpass Gecko.
Comment permalink 4 Bert |
I agree. ASP.Net has made it, i feel, more difficult to get a program-like feel to the web. Javascript, if it were properly standardized is much more appropriate to create events that occur to the user withour having to practically refresh the page every time i click a asp:button. There is alot more flexibility in dynamically creating effects with javascript and no-one seems to give it due credit. .NET developers too shame...
Comment permalink 5 Milan Negovan |
Speaking of dynamically creating effects... There's an interesting post about the distinction between DHTML and DOM.
Comment permalink 6 Prem |
I'm a new buddy to Javascript and this blog site..
Please do put some coding standards and efficient code
for Javascipt and other scripting languages..
thanks...

Emails and Notifications

Would you like to be notified when somebody responds to this post?  Would you like to have these comments emailed to you?

TrackBacks

Sorry, TrackBacks are not allowed.

Submit your comment

Please enter only text since all HTML tags except hyperlinks will be stripped. Hyperlinks will become live links. Any comments with flaming or offensive language will be deleted. Be courteous to other posters. Thank you.

Your name (required):
Your email (optional):
Your site's URL (optional):
Enter this number
Type in the number above:
Comment (required):