Posts Tagged ‘standards’

Targeting Browsers, Why It’s a Good Idea

Monday, March 23rd, 2009

Gone are the days where you need a message like, “This site is best viewed with Netscape 4” on the front page of your site, right? Well maybe, then again maybe not.

Though you don’t need the message anymore, the idea of targeting specific browsers isn’t such a terrible idea. I know, the purists in the crowd are already chomping at the bit to flame me in the comments. Hear me out. I too am a purist at heart. I’m all for cross-browser compatibility, graceful degradation, progressive enhancement and progressive enrichment. But I also believe that a lot can be gained in knowing what browser(s) you’re building your site for. In other words, a study in visitor demographics should be a fundamental step in any site’s planning process. If the project is a site redesign, then a good look at the site’s logs is warranted. If however, the site is to be built from scratch, then a study of the kind of the site’s target demographic is advised.

Why bother with all of this? After all, shouldn’t all sites work on all browsers? Yes, but when you get into the realm of progressive enhancement, it would be nice to know to what lengths you can or should bother to enhance a site. In other words, it’s good to know where to focus your efforts. After all, it isn’t often that you’ll find yourself with limitless budget and time to deliver on a project. Often times you’ll be faced with deadlines and budget constraints and knowing where to devote the bulk of your energy would help in delivering on time and on budget.

I’m willing to bet that if you were to look at the visitor logs of a site like xkcd.com and compare them to those of yahoo.com, you’d see vastly different numbers for visitors who use IE6. That’s because Yahoo caters to a larger swath of the general public whereas XKCD is a comic targeted primarily at geeks. Geeks are well known to regularly update their browsers and are also known to run from Internet Explorer like it was the second coming of the black plague.

So what are some examples of targeting browser behaviour? Well take IE6’s infamous lack of PNG-24 alpha support (that’s where IE6 shows a solid gray rather than transparency in files saved in the PNG-24 format). If you see that a large enough number of visitors to your site are using IE6, you should go the extra mile and rig one of the many patches available to fix the issue. However, since those patches often take time and are cumbersome to set up (to say nothing of the maintenance headache they create), you could also get away with not doing it on sites where the majority of visitors don’t use IE6.

Bottom line, if you’ve got the time and budget to go all out and make your site work perfectly on all browsers, good on ya. But if you don’t then a small study of browser demographics would be a good idea.

Feed Icons Exist for a Reason

Saturday, March 21st, 2009

I just dropped in on the new Seed Magazine site after reading about its redesign on Zeldman’s site. After looking around a little I decided that I’d like to subscribe to its feed so as to keep up with what they publish. But try as I may, I couldn’t find that famous feed icon, or a variation thereof. There’s a reason why standard icons exist, so that people can recognize something like a feed icon on whichever site they may be visiting. It doesn’t help anyone to ditch the icon and go with eight point text instead. Well, unless you really aren’t looking to attract readers.

A Nuance of Preventing Default

Friday, May 16th, 2008

One of the most common operations when assigning event handlers is to prevent the default action the event normally triggers.

In the case of an anchor for example, the click event of an anchor triggers the default behaviour of following the URI specified in the href attribute. So the browser’s default action when clicking the “Fluffy Bunnies” anchor below, is to send the user to http://wait-till-i.com.

<a href="http://wait-till-i.com">Fluffy Bunnies</a>

When coding unobtrusive JavaScript awesomeness however, following the URI isn’t always the desired action that we want when the user clicks on an anchor. Thus, the event handler that is assigned to the anchor needs to stop the default behaviour. This can be done in two ways: by having the handler return false, or using the event object’s preventDefault method (except for in IE which makes you set returnValue to false within the event object).

Returning False

var a = document.getElementsByTagName("a")[0];
a.onclick = function () {
    return false;
};

Preventing Default

var a = document.getElementsByTagName("a")[0];
a.onclick = function (e) {
    e = e || event;
    if (e.preventDefault) {
        e.preventDefault(); // All browsers except IE
    } else {
        e.returnValue = false; // IE
    }
};

Having to use an if/else block every time you want to use preventDefault/returnValue is very cumbersome. This is where a good JavaScript library will serve you well by wrapping this functionality up in one easy function call. The YUI Library for example allows you to do this:

var a = document.getElementsByTagName("a")[0];
a.onclick = function (e) {
    e = e || event;
    YAHOO.util.Event.preventDefault(e);
};

The nuance: using preventDefault in debug vs. release code

In my opinion, preventDefault should be used in two different ways depending on whether you’re in debug mode or release mode. The reason for this is simple, when you’re debugging, the last thing you want is for the browser to leave your page when something breaks on clicking an anchor. Why? Because normally (in Firebug anyway) the error message that was generated disappears once you leave the page. Take the following code example:

var a = document.getElementsByTagName("a")[0];
a.onclick = function (e) {
    e = e || event;
    // Lots of buggy code here
    YAHOO.util.Event.preventDefault(e);
};

In this example, the code will break before preventDefault has a chance to execute. Upon its breaking, the browser will fall back to the anchor’s default behaviour and leave the page to follow the anchor’s URI. That can be annoying when you’re debugging, but it’s exactly what you want in a production environment. You don’t want links to stop working in production when some JavaScript breaks. Rather, you want the script to degrade/break gracefully and give the user access to a valid URI. So the above example is fine for production. While debugging however, the following sequence is preferable:

var a = document.getElementsByTagName("a")[0];
a.onclick = function (e) {
    e = e || event;
    YAHOO.util.Event.preventDefault(e); // Stop the default action before buggy code breaks
    // Lots of buggy code here
};

I hope you’ve found this nuance useful, I know it’s saved me lots of headaches when debugging.

Be Lazy, Let the Browser Do the Work for You

Friday, April 11th, 2008

Imagine you work for a company that has an online product catalog and that your job is to maintain the product images. Now imagine your boss comes to you and says, “here’s a list of a thousand products that need to have ’40% off’ put on them, and we need it for tomorrow, noon.” So you work all night only to have him come to you in the morning and say, “did I say forty? I meant fifty.” Now you’ve got till noon to update a thousand images, go! The more efficient way to handle that situation would be to have a macro, or a template, or some other automated way to either update those images or just superimpose the discount on each of those images.

The moral of the story is, work smarter not harder. When a computer can do the work for you, let it. There’s no glory in doing it yourself. This same moral can be transposed to front-end web development.

There is this thing called “flow” in the browser environment. Think of it as a really crude physics engine. All of the elements rendered in a page are initially a part of the document’s flow. When an image within the flow is enlarged, the text adjacent to it gets shoved over. When it’s reduced, the text is drawn closer. This is good because you don’t need to worry about positioning the text when playing with the size of an image. It’s handled automatically by the browser.

Now imagine if you were to break the flow and opt to position everything yourself. Maintenance would become a nightmare. Sadly this often happens when people get their hands on CSS and don’t know how to properly leverage it. Instead of letting the browser handle the positioning of the elements and just nudging them in place with a few strategically placed rules, they opt to brute force everything into position. Of course, what happens when an image size changes? or a bit of text goes longer than anticipated? Everything breaks. Then they’re up all night fixing a ton of style sheets.

The thing to remember with CSS is: less is more. Really, the cascade works best in conjunction with document flow. Restraining either too much defeats the purpose and leads inevitably to unnecessary headaches.

Microformats, English only?

Monday, March 12th, 2007

Microformats, for those who aren’t in the know, are simply a way to further add semantics to HTML content by using agreed upon class names based on existing models already in use elsewhere. One of the most common microformats–and the subject of this post–is hCard, which is based upon the widely used vCard (RFC 2426) format.

I’ve had the (dis)pleasure of trying to implement hCard on two bilingual sites now only to come to the conclusion that hCard probably wasn’t as well thought out–or to be fair, mature–as I’d have hoped. The reason for it can be seen clearly in the following code example:

<div class="vcard">
  <span class="tel">Tél.:<span class="value">+1.514.555.1212</span></span>
  <span class="tel"><span class="type">Fax</span> Téléc.:<span class="value">+1.514.555.1212</span></span>
</div>

The telephone number’s type value is derived from the span tag’s contents which is displayed in the clear. It is therefore a part of the page’s contents and difficult, if not impossible to justify in a page whose contents are in French–or Spanish, or whatever. Of course, one could always hide the text with CSS, but that’s hardly a solution when separation of content and presentation is concerned.

I raised the issue on the Microformats discussion list and was given a solution that seemed to make sense to me. But shortly thereafter it was pointed out by someone else that the solution itself was a documented issue and not a good solution after all.

In the midst of all of this I came to realize that though Microformats are a great idea and highly useful when they work, the broader issue of internationalization needs to be worked into them if they’re truly to become useful outside of the “English only” world.