Archive for the ‘Uncategorized’ Category

Open Web Camp II at Stanford (July 17, 2010)

Saturday, July 17th, 2010

Open Web Camp II at Stanford today was a blast! Lots of information. Plenty I’m already familiar with, but also lots I learned.

I woke up late, only to see people tweeting about the conference (doh!), so I missed seeing Molly Holzschlag. But I arrived just in time to see…

Estelle Weyl: “CSS3 Implementable Features”

Estelle WeylEstelle Weyl (@estellevw)

Estelle’s talk was about what features of CSS3 are available to use right now. She covered all the usual: border radius, text shadow, box shadow, tilted images, etc. And she showed off Paul Irish’s css3please.com, which greatly helps implement these cross-browser, with even the filter hacks required to pull it off in IE! (no word about the performance hit of these IE hacks, which I’m sure I’ve read about somewhere…)

One of the cool new things I learned was that you can use CSS shorthands to declare border-radius (rounded corner) values. So instead of this mess (courtesy of border-radius.com):

-webkit-border-top-left-radius: 1px;
-webkit-border-top-right-radius: 2px;
-webkit-border-bottom-right-radius: 3px;
-webkit-border-bottom-left-radius: 4px;
-moz-border-radius-topleft: 1px;
-moz-border-radius-topright: 2px;
-moz-border-radius-bottomright: 3px;
-moz-border-radius-bottomleft: 4px;
border-top-left-radius: 1px;
border-top-right-radius: 2px;
border-bottom-right-radius: 3px;
border-bottom-left-radius: 4px;

All you need to do is this, which is equivalent to the above (disclaimer: I’m not sure about the compatibility of this shorthand!):

-moz-border-radius: 10px 20px 30px 40px;
-webkit-border-radius: 10px 20px 30px 40px;
border-radius: 10px 20px 30px 40px;

Or if you get bored of doing a perfectly rounded border radius, you can also do elliptical border radius (again, I’m unsure about the compatibility!):

-moz-border-radius: 20px / 10px;
-webkit-border-radius: 20px / 10px;
border-radius: 20px / 10px;

Matt Henry: “Going Mobile”

Matt HenryMatt Henry (@greenideas)

I’ve worked closely with Matt at Yahoo! Mobile for the past year, so not much was new for me. It seemed that the audience really dug the talk though (lots of questions were asked!).

Essentially, Matt explained that you have to make at least two different versions of your website: one for feature phones and one for smartphones like the iPhone, and with the help of something like WURFL, detect the phone and direct the user to the appropriate site.

Matt also emphasized that feature phone browsers are terrible, much worse than IE6.

As part of a response to someone’s question, Matt talked about the idea of feature detection on smart phones, essentially saying that by the time you’ve sent the JavaScript feature detection to the device, it’s already too late, and in some cases might end up crashing the browser (for feature phones that have an extremely small page weight such as 9KB). So for now it looks like user agent sniffing on the server-side is the way to go.

And there was a brief shoutout for Yahoo! Blueprint, which renders a website across thousands of phones.

Victor Tsaran and Todd Kloots: “Accessibility 101″

Victor Tsaran (left) and Todd Kloots (right)Victor Tsaran (@vick08) and Todd Kloots (@todd)

Before joining Twitter, Todd Kloots spent some time with Victor Tsaran at Yahoo! on accessibility.

In this talk they went over common misunderstandings and showed that even today, big companies such as Digg.com are still not investing in making their site accessible.

This was a pretty high level overview of basic accessibility, including an introduction to screen readers and ARIA attributes.

Here’s some of the stuff covered:
-making forms accessible with fieldset and legend elements and linking captions to their inputs.
-adding alt tags to images even if blank (so the screen reader doesn’t announce the entire URL)
-the title tag doesn’t make things more accessible, whereas the alt tag does
-screen readers obey display: none and visibility: hidden and that to hide something from normal users you have to position it offscreen (with negative left or text-indent).
-how to make basic tables accessible by taking advantage of table summary and caption, as well as putting tables headings into the <thead> element.

I’m still trying to absorb it, but Todd also mentioned that a simple anchor tag for a button didn’t semantically make sense, and that it was more semantic to have a span wrapped around a <button>. I guess I either don’t understand fully or I just disagree, since a <button> appears within a form and sometimes it just may be a cleaner solution to use a simple anchor tag. I guess I will need to look into this more.

Joseph R. Lewis: “Refactoring for Mobile”

Joseph R. LewisJoseph R. Lewis (@sanbeiji)

This talk was basically a live demo of implementing CSS media queries in a WordPress installation to make an iPhone-friendly version of the site. There was a good use of the viewport tag, media queries, @font-face, text-shadow, box-shadow, border-radius, etc.

One surprising thing I learned was that apparently Mobile Safari recognizes border-radius without the webkit prefix? I’m going to follow up on this.

Dirk Ginader: “5 layers of web accessibility”

Dirk GinaderDirk Ginader (@ginader)

Dirk is another Yahoo! (we’re everywhere!) whose presentation was also about accessibility.

As web developers, we’re already familiar with three layers of the web:

-HTML, responsible for content
-CSS, responsible for presentation
-JavaScript, responsible for interaction

Dirk introduced another two levels:

-CSS for JavaScript: special styling for when the full-featured JavaScript is available (this can be used by having JS add a “js” class to the document, and therefore writing CSS rules with “.js” in the front)
-WAI-ARIA: special HTML attributes to help define roles, landmarks, allow the developer to give instructions directly through the screen reader, etc.

Also a few more points:
-use the JavaScript focus() method for events (alerts that are injected at the top of the document and are otherwise ignored by the screen reader, windows that open and close, etc.)
-:hover CSS pseudoclass is for the mouse only.. be sure to also use the :focus pseudoclass! (my note: this is also important on mobile, for touch devices!)

Summary

Overall a great day. The food was pricey but good, and the whole event was free, so I can’t complain! Nice event, I just wish I could’ve seen the other talks in the other tracks!

A Visit From Flickr!

Friday, July 16th, 2010

One of the best perks of working at Yahoo! is getting to attend first-class talks, especially when the talk is by one of your favorite groups. In this case, Flickr!

Flickr schwag: lens cleaner, phone cleaner, or iPhone 4 reception-enabler?Flickr schwag: lens cleaner, phone cleaner, or iPhone 4 reception-enabler?

…and it helps if one of the speakers was once a member of your team (Markus was in Yahoo! Mobile until recently).

Markus Spiering of FlickrMarkus Spiering of Flickr

I probably can’t chat too much about the future improvements they talked about, but I’ll just say there are some exciting plans for Flickr in the coming months! As you may already know, Flickr recently launched its new photo page. Markus went over some of the key new layout improvements, including the new prominence of geolocation and camera model information, which was previously buried deep in the exif info. He and Chris Martin also explained some of the improved photo navigation. Also, for anyone interested to know, the entire site was redone using YUI 3.

Chris briefly went over the public Flickr API they provide.

What’s really exciting is the amount of phones Flickr is being integrated into. It really says something when the top camera model for Flickr is the iPhone. More generally, just think of all the possibilities with packaging Flickr into lots of other phones out there (not everyone has an iPhone, after all!).

Markus was also proud to exclaim that not only was the native Flickr app featured in the recent WWDC keynote, but it was also featured in iPhone commercials around the world (“There’s an app for that…”). Coincidentally, later that day Apple approved the latest version of the Flickr app, which now supports uploading in the background!

Chris Martin of FlickrChris Martin of Flickr

Keep your eyes peeled on Flickr! Cool improvements heading to a screen near you…

A Clockwork Box

Tuesday, March 2nd, 2010
Clockwork Orange cover
With CSS there are always various ways to accomplish something. After reading this short tidbit, you should be familiar with the various ways of controlling the size of an element’s padding, border, and margin, and you should know what the handy “clockwork” tip is, and how it will be useful to remember when you’re putting your CSS into practice.

Equal values on all four sides

If all four values (top, right, bottom, and left) are equal, then you simply write the following:

padding: 1px;
border-width: 1px;
margin: 1px;

The longhand way

If you don’t want equal values on all four sides, then you can specify each side individually:

padding-top: 1px;
padding-right: 2px;
padding-bottom: 3px;
padding-left: 4px;

border-top-width: 1px;
border-right-width: 2px;
border-bottom-width: 3px;
border-left-width: 4px;

margin-top: 1px;
margin-right: 2px;
margin-bottom: 3px;
margin-left: 4px;

The shortcut (like clockwork)

However this seems to be quite a hassle typing out each property, so you’ll find it’s much easier to use the following shorthand, which is in this order: top, right, bottom, left (think of the hands going clockwise around a clock). The following is equivalent to the above code:

padding: 1px 2px 3px 4px;
border-width: 1px 2px 3px 4px;
margin: 1px 2px 3px 4px;

Other shorthands

This is the style I find myself writing in most often, but there are two other shorthand styles you should be aware of:

padding: 1px 2px 3px;  /* top, left/right, bottom */
padding: 1px 2px;      /* top/bottom, left/right */

Summary

In short, there are various ways to define the padding, border, and margin on an element. Here’s a recap, with padding used as an example:

padding: 1px;              /* 1 value: top/right/bottom/left     */
padding: 1px 2px;          /* 2 values: top/bottom, left/right   */
padding: 1px 2px 3px;      /* 3 values: top, left/right, bottom  */
padding: 1px 2px 3px 4px;  /* 4 values: top, right, bottom, left */

Links for the week of February 21, 2010

Sunday, February 28th, 2010

General links

Mobile

Links for the week of February 14, 2010

Sunday, February 21st, 2010

General links

HTML5/CSS3 and new web technology

Mobile

Videos

BlackBerry showing off its new WebKit Browser

Engadget’s hands-on demo of Windows Phone 7 Series

Overall iPhone browser traffic share is decreasing (Jan2009 to Jan2010)

Thursday, February 18th, 2010

According to the statistics available on statcounter.com, from January 2009 to January 2010, iPhone browser traffic share actually decreased, not increased, as one might expect.

This is likely due to new competition from Android phones, as well as the possibility that more users are simply using iPhone native apps instead of web apps.

In any case, in my opinion these are the figures we ought to be looking at, not overall phone sales, as others such as PPK concentrate on. If we don’t use computer sales as an estimate of desktop browser share, then why should we use phone sales as an estimate of mobile browser share? Just because someone has a phone with a pre-installed browser doesn’t lead them to actually use it.

In any case, here’s the statistics, with a few surprises:

  • iPhone/iTouch web traffic share decreased in the US and worldwide
  • BlackBerry gained market share
  • NetFront gained market share (this somewhat baffled me)

And now for the stats…

Worldwide mobile browser traffic (% share)

Device 2009 % 2010 % % change
Opera 24.69 25.53 +0.84
iPhone 23.06 21.52 -1.54
Nokia 17.78 18.53 +1.75
iTouch 12.89 11.6 -1.29
BlackBerry 4.91 9.85 +4.94
Android 1.92 4.54 +2.62
NetFront 1.35 3.27 +1.92
Sony PSP 4.38 1.2 -3.18
Openwave 2.61 0.97 -1.64
(Other) 6.41 2.98 -3.43

US mobile browser traffic (% share)

Device 2009 % 2010 % % change
iPhone 37.01 32.96 -4.05
iTouch 23.49 20.62 -2.87
BlackBerry 11.37 19.32 +7.95
Android 5.19 11.9 +6.71
Sony PSP 7.68 2.18 -5.5
Opera 3.56 3.06 -0.5
Openwave 3.07 2.04 -1.03
NetFront 1 2.26 +1.26
IEMobile 3.12 0.74 -2.38
(Other) 4.5 4.92 +0.42

Predictions for 2010:

  • Android continues to increase market share
  • Decrease in NetFront market share (opposite of current trend)
  • Microsoft Windows Phone 7 Series reverses downward IE trend
  • iPad grows in market share (this is a given, but will it break the top 10?)

Source: statcounter.com

Links for the week of February 7, 2010

Monday, February 15th, 2010

General links

Performance

Events

Pitfalls of declaring variables in JavaScript

Wednesday, November 18th, 2009

Introduction

One of the things that’s always been confusing to me is that there are multiple ways to declare variables in JavaScript, and some ways are better than others. For the beginner programmer, just getting to code to work means complete success, but for the intermediate or advanced programmer, this is just the first step. The next step is to clean up the code and make sure everything is written in the best way it could have been written.

Declaring variables seems like such a basic thing that one almost feels insulted reading about it. Yes, this whole post is about declaring variables. But not just this – it’s about declaring variables the right way. This means declaring variables in the scope they were intended to be declared in.  For the most part this means writing variables in the scope of a function, as opposed to the global namespace (in which everything becomes a property of the window object).

Global variables can be declared in functions

When I was starting to learn JavaScript, I read about global and functional scope and wrongly assumed that functions completely protected the variables declared inside.  As it turns out, there’s many ways to create global variables in JavaScript, and none of them throw up red flags or sound off bells and whistles (unfortunately).

Here’s a few ways to create global variables in JavaScript:

// Creating global variables outside of a function
window.global1 = 1;
global2 = 2;
var global3 = 3;

// Created global variables within a function
function test() {
    global4 = 4;
    window.global5 = 5;
};
test();

You can confirm this for yourself by checking the values of the variables with alert().

Good practice: always precede variables with var

What I learned was that it was essentially good practice to always write variables preceded by the var keyword.  For variables declared outside of functions this has no effect – they are still global, but for variables within functions this ensures that they’re kept within their functional scope:

var global = null;
function test() {
    var local = null;
}
test();

Same name, but different variables

And check this out – confusingly, you can create a global variable, then create a variable with the same name that’s functionally-scoped, yet a completely different variable!

var number = 0;

function test() {
    var number = 1;
}
test();

alert(number);  // 0

When you precede a variable with the var keyword, you’re essentially saying you’re creating the variable for the first time.  So when you create “number” inside the function, no error is thrown because the variable is created in the function’s scope.  If you try to create another variable with the name “number” inside the function, you will get an error because it’s already been defined within the scope.

There’s no such thing as magical protective parenthesis

For some reason I was further mistaken into believing that extra parenthesis around an anonymous function magically protected the variables within:

(function() {
    global = null;
    var local = null;
})();

Nope – as you can see, the global variable is still global since it’s not preceded by the var keyword.  All the extra parenthesis do is execute the function immediately.  That’s it.

Shorthand for declaring multiple functionally-scoped variables

The following code ensures each variable is functionally (locally) scoped, even though only the first variable is preceded by the var keyword:

// shorthand #1
function test() {
    var local1 = null,
        local2 = null;
}
test();

// shorthand #2
function test2() {
    var local1, local2;  // these are of type 'undefined' but they have functional scope!
}
test2();

Multiple variable declaration equivalence gives different scope to each variable

Today I learned another way to mistakenly create a global variable.  Trying to be fancy and declare multiple variables to be the same value, I mistakenly created a global variable (again, no bells and whistles went off to warn me, unfortunately):

function test() {
    var local = global = null;
}
test();

This sets both variables to “null” using the shortest code possible, but unfortunately only the variable “local” is preceded by the var keyword, so only that variable will be in its proper functional scope. Variable “global”, on the other hand, is… well… global. (thanks to Crescent Fish)

You learn something new every day…

Update 1

If you want to check your code for “accidental global variables” you can use JSLint or you can even use Firebug (click on the Script tab, then click on “New watch expression…”, then type “window”, which will display all the properties of the window object – these are all global variables!). Thanks to Nick for the Firebug tip.

Update 2

Just another quick note to remind you that after a variable has been initially declared, the scope will not change. Which is why you can redefine variables, but have them keep their scope:

function test() {
    var local = null;
    local = 5;    // still keeps its local/functional scope
}
test();

As you can see above, the variable “local” keeps its local scope, even though it’s redefined to be 5. This is one of the reasons it’s recommended to declare all variables at the start of a function – to make sure the scope gets set correctly.

One final note: var should only be used when declaring a variable for the first time. If you try to use var for the same variable name in the same scope, it will result in an error:

function test() {
    var local = null;
    var local = 5;    // throws an error!
}
test();

Update 3: Graphical representation

Scope in JavaScript

Scope in JavaScript

JavaScript Tidbit: Block scope with “let”

Tuesday, September 15th, 2009

JavaScript has functional scope. Meaning that if you (properly) define variables within functions, those variables remain accessible only inside the function.

Block scope, on the other hand, defines scope within a block of code, usually defined by braces. JavaScript now has block scope as of version 1.7, which means it’s available in these browsers:

  • Firefox 2+

Block scope is enabled in JavaScript with the use of “let”:

let(x=100) {
    alert(x);
};

It also works perfectly well on one line, without the use of braces:

let(x=100) alert(x);

Note that we can define global variables with the same name outside the block scope and the variables won’t interfere with each other:

x = 200;
let(x=100) alert(x);
alert(x);
// result: 100, 200

Note that there’s a slight caveat – not only is this not available in any version of IE, but it also requires a special script type declaration in order to work (at least for Firefox): type=”text/javascript;version=1.7″

References:
JavaScript Versions
Video: Best Practices in Javascript Library Design (John Resig)
New in JavaScript 1.7

PHP Tidbit: Dead simple singleton

Monday, September 14th, 2009
class Singleton {
    private static $instance;
    public static function getInstance() {
        if(!isset(self::$instance)) {
            $c = __CLASS__;
            self::$instance = new $c();
        }
        return self::$instance;
    }
}

And the explanation…

class Singleton {
    private static $instance;               // static variable to hold our 1 instance

    public static function getInstance() {  // function to get the 1 instance
        if(!isset(self::$instance)) {       // this will only run once (and instantiate once)
            $c = __CLASS__;                 // get the class (Singleton)
            self::$instance = new $c();     // instantiate the class and store it in our variable
        }
        return self::$instance;             // return the instance
    }

    public static function myFunction() {   // we can get to this through Singleton::getInstance()->myFunction()
        // ...
    }
}