Jaffer Haider

September 3, 2008

Google ups the Ante with Chrome

Filed under: Browsers, Google, Web — Jaffer Haider @ 5:54 am

2820302020_eb39fa50e0 Google has officially entered the browser market as of yesterday with their Google Chrome browser. It’s a completely new take on browsers (especially some of the technical aspects unknown to normal users) with a focus on making the browser a faster and safer platform for web applications and to move the focus from the browser to the web site.

I’ve moved to Chrome as my primary browser from the awesome Firefox 3. There are some features lacking in Chrome that I was used to in Firefox 3, but comparing a mature browser like Firefox with a first Beta isn’t really fair. And I’m sure plugins for Chrome will make up for any feature deficiencies, like they did for Firefox 1 and 2.

So far my Chrome experience has been exceptional. The browser loads really quickly and is very responsive. The one feature I absolutely LOVE is the omnibar, great job on that Google! I won’t talk about the features of Chrome in detail, since that’s been done already by a lot of people here (John Resig), here and an article on Internet News.

But before you check out any of those be sure to read the comics introducing Chrome (by Google), they’re very insightful.

1

It’ll be interesting to see how the browser market share shifts. Will Chrome take the users away from Firefox or Internet Explorer? I’m guessing Firefox, since the user base of Firefox is most likely to consist of early adopters. Majority of the people who use the Internet Explorer family of browsers hardly know of any other browser.

I’m very excited, as a web developer, about the paradigm shift that Chrome will undoubtedly bring in the browser market. And with other exciting projects like Prism, IE8 and the awesome stuff going on at Mozilla labs, we’re all set to change the face of the web as we know it.

P.S. check out these Javascript performance comparisons between Chrome and the other browsers. They’re insane!! The V8 JavaScript Virtual Machine incinerates the competition!

June 16, 2008

Firefox 3 – Why so few pledges from Pakistan?!

Filed under: Browsers, Pakistan, Web — Jaffer Haider @ 5:25 pm

Firefox 3 is coming out tomorrow, and this time around Mozilla is going for a Guinness Record for the most software downloads in a day.

So far there have only been 1971 pledges from Pakistan. That puts us in the same category as countries like Ecuador, Belarus, Sri Lanka, Algeria and Egypt. Iran has a whopping 8684 pledges! And they’re right next door to us. (there’s no point in drawing comparisons with India)

Wasn’t Pakistan supposed to have one of the fastest growing tech industries? I thought our software industry and our ‘techies’ in general were a lot more enthusiastic about technology and generally well informed.

And Firefox is more than a web browser, for us web developers it’s a full fledge IDE. I simply can’t imagine web development without Firefox and Firebug. I shudder when I remember the days when we developed applications using Internet Explorer, and pretty much guessed what was going wrong with markup rendering.

So what’s wrong with us? Why are there so few pledges? What factors determine the level of participation in such events? Are we Pakistani’s inherently insular?

UPDATE: At least some Pakistani’s are doing the right thing. The awesome people at WCCFTech have put up a banner and I’m sure they’ve been the source of many pledges.

March 2, 2008

Firefox 3 performance numbers – impressive

Filed under: Browsers, Javascript — Tags: — Jaffer Haider @ 3:15 pm

Stumbled upon these Firefox 3 Javascript performance numbers. Firefox 3 preBeta 4 is about 3.6x times faster than Firefox 2.0.0.12 and a huge 7.5x times faster than IE 7 at crunching Javascript.

I’ve always been pretty impressed by Safari’s js performance, but looks like FF 3 is going to take things to a whole new level.

January 22, 2008

Writing Semantically correct HTML

Filed under: Browsers, CSS, HTML, Web — Tags: — Jaffer Haider @ 10:54 pm

“I see dead HTML tags”

Seriously. Sometimes I’m amazed at how the Web is still able to function with the absolute junk with which web pages are built.

CSS is almost 10 years old, but it still doesn’t render uniformly across browsers (even latest ones). And the fact that it is severely crippled doesn’t help either (you can’t even vertically center text without applying half a dozen hacks!).

JavaScript, the poor thing got crippled during its inception due to the infamous browser wars. It’s precariously carrying the burden of Web 2.0 on its worthy yet twisted shoulders.

HTML … well that’s what this post is all about. HTML on the web has gone through many phases. First there was the plain text phase, with newly introduced presentational tags such as <font> and <color>. Then came the Era of the Table. We’d see tables within tables within tables within tables being used to keep the layout of a page in place. Tables were the easiest thing to use for column layouts back then.

But then came CSS with its call to developers to shun tables and use divs and CSS rules for layout. Most of that stuff fell on deaf ears. So now we see a web where a large number of sites are made by developers who are either infected with divitis, or they haven’t looked beyond tables and inline styles. Either everything is a <div> in these sites, or a table element. Headings, lists, blocks of text, you name it, it’s a div/table.

What I want to list in this post are some rules to remember while writing HTML, such that the resulting HTML:

  • Has semantic meaning. The next developer to work on your markup will get a good idea of what it represents if your HTML is semantically correct and has ‘meaning’ (rather than a huge table within table within table…)
  • Is short and concise
  • Is primarily the structure of your page (as opposed to having presentational and structural properties)

The first thing to keep in mind is to use the correct tag at the correct place.

<h1> – <h5>: These tags should be used for your page name and other section headings. You can set these tags to both inline and block displays to suit your needs. They are SEO friendly as well.

<div>: You should use the divider primarily for layout purposes. A basic rule of thumb should be that if a div itself has any content in it, then you should rethink your use of it, and maybe replace it with a list item (if you’re making tabs or a ‘collection’ of items), span (inline text formatting), paragraph (block error messages) or maybe you don’t even need an encompassing tag for your content (think unnecessary boxing of images into divs).

<ul>, <ol>, <li>: Very versatile tags, should be used for both vertical and horizontal (tabs) lists. Also consider using lists for structures of data such as galleries, menus and draggable items. They work really well when floated as well.

<span>: Tag of choice for formatting inline text. It is essential however that you know that you don’t have to use it every time. There are other tags, such as <i>, <pre>, <dfn>, <code>, <strong> etc that you can use in most cases. Remember that you should use these tags according to the context of your site, e.g. <strong> doesn’t necessarily have to be ‘bold’ text. You can give it any style that applies to emphasized text in your page’s layout. Note that these tags will give structure to your data, through CSS you will set their presentational properties, and actually define what, say a <strong> tag will look like.

<label>: I’ve seen many forms that don’t use this tag. I’ve seen JavaScript that is doing what this tag automatically does for you in your forms (click sets focus to related input field). This should not be so. Your forms should always make use of this tag to describe what the user should input in a field. Be sure to use the ‘for’ attribute to link a <label> to its <input> tag.

<p>: This tag should be considered when you’re writing welcome/instructional/advert text on your webpage. Note that usually this text is encompassed in a div or other structure. <p> works just fine in these cases.

<table>: Use for displaying tabular data only. Remember to use the <thead> and <tbody> children tags.

<a>: Use for links. Also work really well in block display, so consider using them where you might be using divs with onclick events and cursor set to pointer.

These are the most commonly used tags out there. Be warned that you will need to swallow the bitter pill of CSS to make full use of some of the above tags, especially divs and list items.

Hopefully this rant makes some sense to you and will help you in structuring your pages better. If you think I got something wrong, comment and be heard!

December 29, 2007

Fix for the Firefox/CSS one page printing bug

Filed under: Browsers, CSS — Jaffer Haider @ 1:11 pm

I ran into Firefox’s CSS printing bug that would only print one page and let the content overflow (without clipping the page). The actual bug is documented here by Mozilla.

After scouring the Net for a fix, I found that it can be caused due to a number of reasons. Here’s the list of fixes that should help you out if you’re facing this bug:

Floating Elements
You need to unfloat all floated elements in your CSS for print media, like so:

.my-floated-elements {
float: none !important;
}

Overflow
Overflow must be set to visible for elements that have might have fancy overflow settings:

.css-element {
overflow: visible !important;
}

It’s always good to set this property for the <body> tag (or any other topmost parent element that is housing contents to be printed). Additionally, in some cases, overflow: scroll might work in some cases instead of overflow: visible. It greatly depends on your layout.

Position
If your elements are positioned absolutely, then you might need to position them relatively:

.css-element {
position: relative;
}

Height
If the height of your body is set to 100%, that may also cause a problem. Try unsetting this property.

UPDATE
Thanks to Gérald Champavert for pointing out that  setting width to ‘auto’ for container divs might also help in solving the problem.

May 11, 2007

Writing better Javascript – The Essentials

Filed under: Browsers, Javascript, Web — Jaffer Haider @ 1:01 pm

When I came across Javascript for the first time, it was in the form of a snippet that I put on a page to do some validation. Most web developers start out with Javascript in the same way. Do a little validation here, a rollover effect there , trivial stuff like that. That gets most people started with Javascript on the wrong foot, thinking that it’s a mere scripting aid.

With more and more Javascript code being released on the web, and with more applications relying on Ajax and Javascript effects in a bid to jump onto the Web 2.0 bandwagon, it is really important that people starting out with Javascript are aware of its strengths, weaknesses and quirks before they actually write production code.

So here’s my attempt at making the lives of Javascript beginners a little easier.

Know the Language
Before you can actually write quality code in a language, you have to know it inside out. Fortunately, Javascript is not a huge language to cover. It is hover, very versatile. The expressive power that you have at your disposal when you’re coding in Javascript is amazing. I can ramble on endlessly about how awesome Javascript is, so I’ll get right to the point. The best reference IMO that’ll get you started in the right direction is this 3 part series of lectures by Douglas Crockford. He’s currently a Senior Javascript Architect at Yahoo!, and his lectures are very nicely organized and his delivery style keeps you interested. Also, if you’re interested in the history of the language and the browser wars that played a great part in shaping how we code for the Web today, you’ll love these lectures. Even after sitting through these lectures, I still use the power point slides for quick reference whenever I’m unsure about something.
Sit through these lectures with the intent of learning something and by the time you finish, not only will you be crazy excited about Javascript, you’ll have a solid foundation to begin writing code and building on what you’ve learnt. For me, these lectures have defined how I write Javascript code. The difference between the code I wrote before and after these lectures is staggering. So if you don’t know why you should use === and !== instead of == and !=, or what a lambda or closure is, or how to do prototypal inheritance, I suggest you click here.

Unobtrusive Javascript
If you’re into the whole separation of behavior, content and presentation on the web thing then you’ll know what I’m talking about. If you don’t know what the previous sentence meant, you need to crawl out from your hiding place under a rock. There are many schools of thought on unobtrusive Javascript. I being a firm believer in KISS (Keep It Simple Stupid) have a don’t overdo it approach to unobtrusive Javascript. If it’s helping me out, fine, if a certain thing is too hard to code without having Javascript in the HTML, so be it. Please forgive me my unobtrusive fanatic friends.
So what you basically need to do is write all your Javascript code in a separate .js file. There are many advantages to this technique.

  • All your Javascript is cached in one go, leaving only the HTML to be fetched from the server on each page. (This depends on how you’ve organized your .js files)
  • Code reusability.
  • Easier to organize your code in packages/classes. You can write a controller for each page that will initialize its Javascript components, or better yet, have a base controller which initializes common components and all other controllers inherit from the base controller.
  • Improvement in code readability, no more HTML strewn with Javascript.

Time to write some code. So say that you have an image that has a rollover effect. If you had been coding 5 years ago, you would have done something like this:
<img src="button.gif" onmouseover="this.src='button_over.gif'" onmouseout="this.src='button.gif'" alt="" />

Say you have twenty such buttons all over the page, you’d have to do this to each one of them. Here’s how to do it the unobtrusive way (simplified code):
HTML
<img src="button.gif" class="rollover" alt="" />
Javascript
function activateRollovers(){
var rollovers = document.getElementsByClassName('rollover');
for(var i=0;i<rollovers.lenght;i++){
rollovers[i].onmouseover = function(){
// insert '_over' in image name before image extension
};
rollovers[i].onmouseout = function(){
// remove '_over' in image name before image extension
}
}
}
// call this function when window loads (prototype.js method)
Event.observe(window, 'load', activateRollovers);

CSS class names are most commonly used for the purpose of grabbing elements of a certain type, but there are other methods as well, described in this example of unobtrusive form validation.

Objects and Functions
Everything is an object in Javascript. A function is an object. You can even define a function as a variable. What makes Javascript objects truly unique is that they are essentially Hashmaps. Objects are comprised of name <-> value pairs, where the name is a string and the value is an object. Master the functional aspects of Javascript (functions as variables & objects, passing functions, function pointers), and you’ll be on you’re on your way to Javascript greatness.

Frameworks and Libraries
It is pretty well known that there is no dearth of Javascript frameworks and libraries. The web is overflowing with them. Choosing the right on for your project is the important bit.
But why use a framework at all you say? Javascript has its share of quirks and flaws. And inconsistent implementation and incompatible APIs by browsers doesn’t help either. The extensible nature of Javascript (you can modify built in objects via their prototypes) allows frameworks to provide richer built in objects and cross browser compatible methods. Most frameworks provide Ajax objects, which allow easy communication with the server.
I’ve personally always used the prototype framework. Yahoo!’s YUI has a rich set of UI controls, and is a good choice if your site has heavy Javascript/Ajax UI work. jQuery, mootools and dojo all come highly recommended. Read through Justin’s Top 5 Javascript frameworks list to get a clearer view of the playing field. Although I disagree with his ranking, I found the comments to the post enlightening.

Ajax Patterns
Ajax IMO is a necessary evil. I’d rather work in Flex, or maybe even Silverlight for RIA. (Can’t wait for JavaFX!) But as long as there’s a demand for DHTML, you’re going to have to write Ajax code. There are a gzillion different ways of doing a thing using Ajax, so I highly recommend using Ajax Patterns as a reference for all your Ajax design and coding time decisions. If you’re going to do something wrong, do it right.

Exception Handling
As I’m writing this post on the Posts page in WordPress, Firebug is alerting me to a Javascript error on this page. The web has more Javascript errors than it actually has web pages. Probably. My point is that exception handling is rarely applied in Javascript code on web pages. The fact that Javascript is loosely typed is probably one of the main reasons why programmers get sloppy and don’t feel that their Javascript code needs as much exception handling as their Java/.NET code.
Javascript exceptions in the worst case can render unusable UI on a web page. Using exception handling will allow you to show something like an error message or report bug form in place of the UI, instead of something broken. Or maybe even switch to a non-Javascript UI on an exception. The sky’s the limit.

I hope this rant has set you off on the right foot with Javascript. Treat your Javascript code as more than a code snippet, and your code will be significantly more robust, efficient and manageable.

February 14, 2007

Link CSS class ignored in IE – quick fix

Filed under: Browsers, CSS — Jaffer Haider @ 8:39 pm

If you have a CSS rule like

body a {
/* default styling */
}

and there’s a case where you want to apply a different class to an anchor (link) by doing

CSS
a.myClass {
/* new style */
}

HTML
<a href="foobar.html" class="myClass">....</a>

then your efforts will be shot down by our good ol’ friend IE 6. To make it work in Internet Explorer, one technique is to use the id selector for your classes, like so

CSS
#linkContainer a {
/* new style */
}

HTML
<span id="linkContainer">
<a href="foobar.html">....</a>
</span>

You can use any other tag in place of the span, but I prefer it since it doesn’t have an effect on the layout mostly.

February 8, 2007

Images with border effects and onclick – IE problem

Filed under: Browsers, CSS, Javascript, Web — Jaffer Haider @ 1:25 pm

These days I’m working on an Ajax driven photo customization UI where the user can select from a range of frames that they can put on the photo. In the middle of the page is their photo, and in the right corner there’s a scrollable table with frames (2 in each row).

Everything’s hunky dory until I decide to add a border effect on frame images, so that on mouseover (or hover in CSS) the border increases (pretty standard stuff). I go ahead and add the following in the stylesheet and code respectively:

CSS:
img.frame {
border:#80A7D2;
border-width:1px;
border-style:solid;
margin:3px;
}
img.frame:hover {
border:width:2px;
margin:2px;
}

HTML:
<img class="frame" src="foo" alt="bar" onclick="doStuff()"/>

Problem
This gives me a nice border effect and the onclick of the <img> works as expected in FireFox. But when I verify this in IE, there’s no border effect whatsoever.

Solution Part 1
So I look at some legacy code elsewhere in the application, because there are areas in which this border effect is being used (albeit slightly differently), and what results is an encapsulation of the <img> tag with an <a> tag. The style is also changed to apply it to the anchor instead of the image:

CSS:
a.frame_holder {
border:#80A7D2;
border-width:1px;
border-style:solid;
margin:3px;
display:block;
}
a.frame_holder:hover {
border:width:2px;
margin:2px;
display:block;
}

HTML:
<a class="frame_holder" href="#"><img src="foo" alt="bar" onclick="doStuff()"/></a>

The border effect doesn’t work unless you set the display type to ‘block’. Another problem is the href=”#” which scrolls the page to the top if you click the link. Now that’s perfectly acceptable if you’re going to another page on clicking the link, since the scrolling never happens. But while using Ajax, you don’t want the screen to be scrolling to the top in the middle of an action.

Solution Part 2
What I eventually did was to replace the href=”#” with href=”javascript:;”, which is basically an empty line of Javascript. This stops browsers (tested on FireFox, IE 6 and Safari) from scrolling to the top.

So I end up with
<a class="frame_holder" href="javascript:;"><img src="foo" alt="bar" onclick="doStuff()"/></a>
instead of
<img class="frame" src="foo" alt="bar" onclick="doStuff()"/>
to make it work in Internet Explorer 6 (I have yet to test it on IE 7 *crosses fingers*).

UPDATE
Added reference to ‘javascript:;’ in the href attribute instead of ‘#foobar’. Thanks to Wade for reminding me. I was pretty green when I wrote this post, am a lot wiser now :)

October 14, 2006

Browser Compatibility

Filed under: Browsers, Web — Jaffer Haider @ 10:39 am

Found this nice article on browser compatibility while finding out what’s the difference between .html and .html files. Was a good read. Browser Compatibility Tutorial
Oh and there’s NO difference between .html and .htm files, except for the ‘L’ :P

Blog at WordPress.com.