Archive for the ‘Best Practices’ Category

Beware: object = object has a pitfall

January 12th, 2007 by Aaron N.

So I spent an entire day discovering a quirk about javascript that I must now share. In a previous post on creating default settings for classes/objects I discussed the following technique:

var Widget = new Class({
	initialize: function(element, options){
		this.element = element;
		this.options = Object.extend({
			offsetX: 0,
			offsetY: 0
		}, options || {});
		this.setPosition();
	},
	setPosition: function(){
		this.element.setStyles({
			left: this.options.offsetX + 'px',
			top: this.options.offsetY + 'px'
		});
	}
});

Now, this isn’t a very useful class, but it illustrates the technique. The functions in our class don’t have to worry if the options are defined; they are either what the default value is or they are what the user passed in. If the user elects to just pass in a subset of the values, that’s fine:

var myWidget = new Widget(myElement, {offsetX: 100});
//myElement will be offset by 100 on the left,
//zero (the default) on the top

But what if you want to extend the functionality of your class later? What if you want to be able to insert more default options?

Here’s what I was doing that caused me trouble: Read the rest of this entry »

Performace posts - CNAMES, compression, concatenation

December 20th, 2006 by Aaron N.

So I’ve been very busy the last few weeks. I’m rewriting, well, pretty much all the javascript on Download.com. Fun! I’ve written a few new classes that I think will be helpful; more on those later.

In the mean time, here are two posts from Ajaxian that are worth a read. The first is on using CNAMES to allow your browser to serially download javascript and css, while the other is on compressing and concatenating files.

Note that Firebug 1 (beta) will let you actually see firefox download all your files and see the order and time each took.

Also note that our new framework environment does the compressing and concatenation stuff, but it’s still interesting to see the work that others are doing.

Aaron

Back Button Support: Safari and Speed

November 16th, 2006 by Aaron N.

via Ajaxian:

Daniel Kantor has implemented a Back button solution in Streampad and has shared it with us.

One of the main gripes against AJAX webapps is how they break the back button in a typical browser. There have been a few solutions (notably Brad Neuberg’s Really Simple History) but none have got it working in Safari. GMail still does not have a working back button in Safari.

They say the third time is the charm and I have tried to get a Back button thing in Streampad twice before. I do not want to use someone else’s library as they are usually more complex than I need and I did not want to put something in place until I had Safari working. I tried a few different techniques, but when I got it working in Safari, it would break in Firefox or IE.

I finally figured out a way to get this working in Firefox, IE and Safari.

While Daniel struggled with this, he found that the back button support caused a slow down in the entire app performance. He came up with a new solution that didn’t suffer from the performance issues:

The general concept is this: You load a page into an iframe that calls parent.goBack() and then changes its own location to a new page (a blank page will do). Because the page jumps to a new location, it now has a history. If you click the back button, it will load the page again (calling parent.goBack()) and then spring forward to the dummy blank page.

var historyArray = Array(); // create an empty array to hold the history
var historyCounter = -1; // initialize the array pointer to -1

function historyAdd(f){
  if (historyCounter == -1){  // the first time this is called it will change the iframe location
  document.getElementById(’hFrame’).src = “/historySpring.php”;
}

var o = historyArray[historyCounter];
if (f != o){ // don’t put in consecutive duplicates
  historyCounter++
  historyArray[historyCounter] = f; // add function to history
}

function goBack() {
  if (historyCounter> 0){ // don’t want to call it if there is nothing in history array
    historyCounter– // set the pointer back one
    var f = historyArray[historyCounter]; // get the function from the history array
    f = f+”()”;
    eval(f); // call the function
  }
}

this.options - setting defaults that can be overwritten in your classes

November 14th, 2006 by Aaron N.

Here’s a trick I learned from Valerio (author of Mootools) on how to handle options for classes and functions. After refactoring a couple of legacy scripts I thought I’d share for those of you who might not know it already. This example below uses the Mootools syntax for class creation, but the rest of it can be used even on its own I think. Read the rest of this entry »

Optimizing Page Load Time

November 1st, 2006 by Aaron N.

via ajaxian (as usual):

Aaron Hopkins of Google has released an article on Optimizing Page Load Time which came out of his experience optimizing page load times for a high-profile Ajax application.

He starts off talking about “how much I could reduce latency due to external objects. Specifically, I looked into how the HTTP client implementation in common browsers and characteristics of common Internet connections affect page load time for pages with many small objects.” Read the rest of this entry »

IE7: Twice as performant as IE6, but half as slow as FF 1.5

October 26th, 2006 by Aaron N.

via ajaxian: IE7: Twice as performant as IE6, but half as slow as FF 1.5
Ross Dargahi of Zimbra complained about IE 6 performance. Since IE 7 is here, he decided to retry his tests and see how Microsoft had done:

Microsoft’s IE team has clearly been hard at work on improving their browser’s memory management and JavaScript performance. IE 7 has made some significant leaps forward based on some initial in house testing here at Zimbra. We are in general observing about a 2x performance improvement with IE 7 vs IE 6 when using the Zimbra Web Client (ZWC). Read the rest of this entry »

Detecting IE7+ in javascript

October 26th, 2006 by Aaron N.

via ajaxian:

Abe Fettig knows that we need to start detecting the difference between IE6- and IE7+, because a lot of the hacks that we were using for IE are no longer needed.

He didn’t want to grok the user agent, as that is very brittle, so he came up with:

if (typeof document.body.style.maxHeight != “undefined”) {
// IE 7, mozilla, safari, opera 9
} else {
// IE6, older browsers
}

Any other thoughts?

Read the rest of this entry »

Bill Scott on Designing a Component

October 26th, 2006 by Aaron N.

Bill Scott has a great article on designing a component that is worth a read. He talks about his rules of thumb when building something that should be reusable (and nearly everything should):

  • Make the component specific in purpose, yet flexible in use.
  • Avoid the do-everything component. Instead make it do the main thing well. But make it pragmatic by keeping it flexible.

  • Separate the concerns of interface, interaction, and content.
  • Avoid hard-coding visual style, give flexibility in the interaction model and provide flexible ways to manage data content. Keep these three areas independent to allow them to be customized separately.

  • Document the interface and use the component before releasing it.
  • You know the experience. You think you have a great idea. Then when you go to explain it to another person you immediately see the holes in your logic. Documentation provides a way to explain your interface; writing demos allow you to exercise the component to test its ease of use and flexibility.

There’s a lot more detail and examples in the full article.

This kind of thinking has had a big influence on my work on CNET’s global framework and stumbling across this article just recently made me refocus my thoughts on the topic.

a.n.

IE-7 standalone (and, for that matter, IE 3, 4, 5, and 6)

October 26th, 2006 by Aaron N.

Want several copies of IE running side by side? Want to test out IE 7 but don’t really want to zorch your IE 6 that you also need to test? No problem.

Dean Edwards /packer/ compression tool

October 26th, 2006 by Aaron N.

We’ve been working pretty hard on CNET’s new javascript global framework. It’s based on Mootools with a bunch of other things mixed in. Last week I took on the task of documenting the entire Mootools library, now online. I struck up a conversation with the author of that library, Valerio Proietti from mad4milk.net and have since been granted access to contribute to the source of that library (thanks Valerio!). Mostly my contributions have been focused on the docs, but I’ve also helped out with a few bugs and the like.

Anyway, one of the things that Mootools turned me on to was Dean Edward’s /packer/, which is an awesome javascript compression tool. This thing compresses our libraries dramatically, sometimes as much as halving their file size. We’ve actually put our new global libraries into production on news.com and I just got around to actually testing the efficiency of the compression algorythm. Read the rest of this entry »