Archive for March, 2009

Reducing JavaScript – Omitting Curly Braces

Edit: This “trick” is used in a lot of JavaScript compression tools or pre-made scripts, like Adobe Dreamweaver’s JavaScript roll-over images.

Note: This is actually a JavaScript trick that was, I would think, never meant to be actually executable. Do not use it in production environments, it’s unstable and clearly more confusing than useful. It’s just cool to know.

JavaScript Browser Support

JavaScript has always had its share of standardization issues. From Microsoft’s JScript to even Adobe’s ActionScript, they are all, afterall, just ECMAScript Dialects (well, since 2 years after JavaScript’s invention, in 1997).

In any cases, some very obscure less documented stuff exists in JavaScript, often akin to indivial dialects of ECMAScript. One of them is the omission of curly braces in conditional statements and functions.

Omission in conditional statements actually looks like it’s standardized since all ECMAScript dialects support it perfectly (JavaScript, JScript, ActionScript 2.0, ActionScript 3.0). However, it heavily lacks documentation and is often omitted entirely from books.

On the other hand, curly braces omission with functions will not work in anything else than Firefox. This is no surprise though, since Firefox supports a much more recent version of JavaScript than other browsers, although I doubt that functionality is actually part of ECMAScript.

As of Firefox 3.1, it supports version 1.9, compared to 1.5 which is standard on KHTML/Webkit/Chromium browsers like Konqueror, Safari and Chrome. Opera and Internet Explorer have version 1.5 and version 1.3 respectively, but they cannot really be judged on their JavaScript version since they both use custom JavaScript builds. Opera uses a combination of standard ECMAScript support plus JavaScript and JScript extensions, providing it with greater support for Internet Explorer-only scripts. On the other hand, Internet Explorer uses JScript entirely, Microsoft’s own version of JavaScript, which adheres in its own ways to the ECMAScript standard.

What’s a curly brace (or bracket)?

Note: In every example, omitting the semicolon at the end of a statement is totally possible and will not influence the functionality of the code in any way. Despite what it might look like in these examples, a semicolon is purely stylistic in JavaScript and is only used here for the sake of familiarity (because most programmers write like this and it’s a good practice to do so for languages like PHP that do require them).

In case you missed on the curly brace thing, this is a curly brace set: { }

With Conditionals

Following is traditional if conditional statement.

if (boolean == true)
{
  alert("test");
}

Next up is the same construct without the curly braces.

if (boolean == true)
  alert("test");

Both of those work exactly alike. Much in the same way, if else conditional statements can be used.

if (boolean == true)
  alert("test";
else
  alert("test2");

And so, this short-hand like syntax can also be used with conditional loops like for and while.

With Functions

Albeit not being supported in anything else than Firefox, it’s still interesting to note that functions will also work without the curly braces, like this.

function myFunction()
  //code to execute;

Particularities

However, it’s important to know the omission of curly braces does not function exactly like regular statement blocks. In fact, it is only possible with one statement, after which the other statements will be executed as if they were out of the condition of function. This is due to JavaScript’s ignorance of Line Returns and Tabulations. Don’t forget JavaScript reads everything as one line, so the indentation shown here simply illustrates how useless it is to the code’s contructs.

The following two examples produce the exact same output.

With Curly Braces

var myVar = 10;
if (myVar == 10)
{
  document.write("myVar is equal to 10.<br>");
}
document.write("This block executes even if the if condition is not met.";

Without Curly Braces

var myVar = 10;
if (myVar == 10)
  document.write("myVar is equal to 10.<br>");
  document.write("This block executes even if the if condition is not met.";

Did you know empty tags in HTML were actually closed!?

As you know, in XHTML there is what we call self-closing tags, or empty tags if you want. One notable example is <img /> or <br />. However, since XHTML really is XML, <img></img> and <br></br> is also possible.
Note: The <br></br> is only correctly interpreted in Firefox and Safari.

But, big surprise, this is not possible in HTML! Wait, huh? Why wouldn’t it be. <br> and <img> alone just look like tags that aren’t closed, but that only applies to XML. In HTML, empty tags like <img> and <br> are closed, and typing <img></img> will give you a wonderful validation error saying no opening tag for </img> could be found.

So next time somebody says this while you’re editing HTML:
- Watch it man, that’s supposed to be a non-closed tag
You can answer:
- Uh huh, empty tag! (or self-closing)

BTW: I’m positively thinking HTML’s way of doing this doesn’t make sense. Who’s the idiot? (actually we know who) And that XML does away with that closing concept in a much more logical and consistent handling, I mean, if <img> just like that is already closed, why would <p> wouldn’t be?

USB Key—Permission Denied; What to do

Have you ever searched forums a night long just to find stupid answers like “Did you restart, check your drive letter, etc.” because any OS would tell you that it cannot write to or format your USB key because the permission was denied? If yes, here’s a solution you may have never thought of:

Check on the physical body of your USB key, maybe there’s a write protection switch turned on. Yes, as SD cards do, some USB keys are uselessly equipped with that hardware feature. And by uselessly, I mean that it causes more problems than it solves them because it can become loose, or you could turn it on by accident.

Cause you probably searched for it—Windows XP Tip

Yes it’s about XP. It’s old, but a lot of people still use it, and sometimes there’s some stuff that’s really deep in there. So, here’s the story.

At work, I had this very annoying default that every explorer window would open to; that is, with the folders view on. And there was no way to change it. Further more, turning off the folders view made any folder open in another window, with folders view on.

So, here’s how, in case you searched for it:

> Explorer Window > Tools > Folder Options
>  File Types > Folder > Advanced

And set default to open instead of explore.

Right, not too hard, but definitely awkward place for such a setting.

Is Internet Explorer 8 really fast? What the test doesn’t tell.

Note: Some people might inadvertadly think I’m declaring Chrome as a superior browser. However, the reality is I’m only talking about speed. There’s dosens of reasons I don’t use Chrome myself as my everyday browser on Windows. Keep in mind this article is only about speed. In my opinion, Internet Explorer 8 is superior to Chrome functionality-wise.

Recently, SuperSite for Windows’ author Paul Thurrott posted an article about Microsoft’s Video on IE’s speed. While definitely cool, the truth is far from being as acceptable.

Turns out that test, as many tests in fact, doesn’t account for a lot of things. Ask anyone that tried Chrome which browser is faster, between Chrome and Internet Explorer, and they’ll say Chrome was faster. Considering Microsoft’s claims, it shouldn’t be so.

In the video, they spend a lot of time explaning that Internet Explorer is faster on one third more of the 25 most popular websites than Chrome. While not a large margin, the 25 most popular websites are far from representing the bulk of today’s web technology. You have to consider JavaScript and plugins on sites like Gmail and YouTube.

But wait, isn’t YouTube in the most popular sites? Well, its home page is, but the rest of the pages aren’t. Microsoft’s test never did extended reports on YouTube video pages alone.

Besides every little thing that make the Internet fluctuate in speed, even if local, a 0.02 seconds difference in any test is far from telling anything. It does back a good point though; any noticeable difference between loading Google on Chrome and on Internet Explorer is bogus.

And it all comes down to page-load time. Is that all there is to it? I think this is where every web browser benchmark fails. They each fail to detail information beyond the web page, like opening a tab, managing bookmarks, etc.

Just try opening a new tab in Internet Explorer 8, then open one in Chrome, the gap is undeniable. And it goes without saying that it is the same for a lot of things in Internet Explorer, giving it a definitely slower feel than its competitors.

However, Internet Explorer still is being blamed more than it should be. Firefox never was quite faster, neither was Safari on Windows. And another note, even though Internet Explorer ends up last in every JavaScript benchmark, it’s interesting to note how it manages to handle plugins, like Flash, with considerably less CPU than others. On older machines, I’ve had repeated cases where Acrobat.com would be laggy on Firefox and fast on Internet Explorer.

When it comes down to it though, Chrome can easily be declared as the fastest browser.

Shorthand conditional statements

This article uses ECMAScript as its example language. See bellow for language-specific information. The shorthand conditional statement, or ?:, is also know as the ternary operator.

Although it exists in virtually all programming languages, the conditional operator ?: remains a relatively obscure part of any language. Surprisingly enough though, it remains a very useful operator can save you a bunch of lines when you don’t need the complex stuff.

However, its broad ommitance from books and codes remains justified, ?: is far from being as readeable in comparison to its cousin if. But that’s to take for granted, since it is afterall a shorthand statement.

if (new Date().getHours() < 11)
{
    var timecode:String = "AM";
}
else
{
    var timecode:String = "PM";
}

Traditionally, the example figured just up, using the if conditional statement, would be used. However, such a simple conditional statement shouldn’t necessarily take up 8 lines of code and can instead be stuffed in a magical one line statement when declaring your timecode variable.

var timecode:String = (new Date().getHours() < 11) ? "AM" : "PM";

Although limited to an if else form, the ?: can be  super useful for very quick conditional assignements on a single line. The usage is simple:

boolean-value ? if-true-this-executes : if-false-this-executes ;

So yes, it does the exact same as an if else. If you still have difficulty grapsing on the concept of ?:, the following if else conditional statement has been converted to fit in a ?: conditional statement. Note however that this form is incorrect, it is just for comprehension of the functionality. For additional visual cues, the second line represents the correct form, with color-coded magic so you can clearly understand, and the rest represents a correct if else conditional statement to compare.

(boolean-value) if { when-true } else { when-false };
 boolean-value  ?    when-true   :      when-false  ;

if (boolean-value)
{
    when-true;
}
else
{
    when-false;
}

Here is a list of languages that enable this form, and specifics about them. Despite the if else conditional statement varying significantly amongst languages, the ?: is surprisingly similar.

Ruby
They call it a short-if statement. Uses the exact same syntax as shown in the colored example but ommits the statement end semicolon entirely.

ECMAScript
Every ECMAScript compliant dialect (Javascript, JScript, ActionScript, etc.) should support the ?; conditional statement with the exact same syntax as shown above. Semicolon is optional.

PHP
Uses the exact same syntax as shown above in the colored example. Semicolon is mandatory.

Java
Uses the exact same syntax as shown above in the colored example. Semicolon is mandatory.

C#
Uses the exact same syntax as shown above in the colored example. Semicolon is mandatory.

C
Uses the exact same syntax as shown above in the colored example. Semicolon is mandatory.

C++
Uses the exact same syntax as shown above in the colored example. Semicolon is mandatory.

Objective-C
Uses the exact same syntax as shown above in the colored example. Semicolon is mandatory.

VB.net
Uses a different syntax:

IIf(condition, when-true, when-false)

Python
Uses a diffrent syntax:

when-true if boolean-value else when-false

How the face of the web is changing—From 2004 to 2009

Statistics are rounded for stylistic reasons.

In 2004, Google had 58% of the search market. Firefox had only gained 4% of the browser market and the best you could hope for is terms of web technology was what IE 6 supported.

In 2009, Google has 82% of the search market, Firefox 22% of the browser market, and today’s most recent browsers are all Acid 2 compliant.

It is undeniable that in just a little more than 4 years (as per the current time of posting of this article), the very face of the web has dramatically changed. Stuff we take for granted today, like YouTube, didn’t even exist back in 2004 (YouTube was launched in 2005). Sometimes realizing at what point the web industry moved is simply alluring. No wonder ours parents can’t follow (the baby boomers generation).

One’s thing for sure, you’ll have to keep learning new stuff all the time if you want to keep up.

Something I would find important to note is that it’s only 4 years. Was Microsoft that slow? Or has it just all happened so incredibly fast? On another front, you really have to think that Windows XP, released in 2001, and it’s latest functionality change, SP2 in 2004, is really old (SP3 did not provide any significant functionality change).

Fake Bold >> More control

HTML has a few tricks up its sleeve and it stays a relatively difficult task to create accessible websites with it. Nevertheless, accessible HTML is probably the single most accessible content in the world. In any cases, one wrongfuly overlooked area of accessibility is bold and italic.

In most people’s mind, suffice is to wrap any bold or italic text in proper XHTML tags. For some other more advanced fellows, proper XHTML tags also include older forms like <b> and <i>, which, like CSS, have only one purpose, style, and won’t be read differently by screen readers.

Why would anyone want to use bold when it can’t be read by a screen reader? Well, I’ll let you decide, but here’s a hint: you don’t neccesarily want the screen reader to shout your text just because you want it to “look” bigger.

In any cases, when using semantic tags, like <strong> and <em>, what you really do is define a “strong emphasis” for <strong> and a regular “emphasis” for <em>. But, what if you don’t want to use italic, what if you have multiple “bolds”, and what if you just want bold to be an very regular emphasis in your text intead of a “strong emphasis”? You make a fake bold!

<em style="font-weight:bold; font-style:normal;">emphasis</em>

I think that’s sexy. First off, style is totally ignored by a screen reader, so the screen reader will read this as its tag, an <em>, so a regular “emphasis”. However, we removed the italic (font-style:normal) and added a bold appearence (font-weight:bold), which means our text will display as bold for people with eyes, and a simple emphasis for semantic-aware browsers like screen readers.

Problem solved.

The Simpsons; The Standard American Family Now in HD

Although the Simpsons’ adventures are far from typical of everyone’s daily lives, the larger image around it certainly is. Somehow, the Simpsons have always represented what the average American family was and, more particularily, what it should be.

Recently, the Simpsons have began broadcasting in HD, along with a brand new opening sequence in all its high definition glory. Although this is a natural evolution, the new version of the Simpsons has a particularily eerie presence of HDTVs in it.

And there is a reason for that. In preperation for the United States’s pushed back digital transition, and in migration to a more sophisticated standard–HDTV–along with the poor economy, there’s nothing like a classic cartoon nobody doesn’t know about to push HDTVs into households and make people spend.

From Bart’s writing on the Chalkboard and the flat panel HDTV in the Simpsons’ living room, the message is pretty clear, having a flat-panel HDTV in your living room is the new standard.