Zachary Johnson has put together another fun demo. He’s using some JavaScript and the new text-shadow property to build a spotlight effect. It’s embedded below. If you can’t view it, click through to his post.
Daily Archive for June 25th, 2009
This post was written by Jeff Balogh. Jeff works on Mozilla’s web development team.
New in Firefox 3.5, localStorage is a part of the Web Storage specification. localStorage provides a simple Javascript API for persisting key-value pairs in the browser. It shouldn’t be confused with the SQL database storage proposal, which is a separate (and more contentious) part of the Web Storage spec. Key-value pairs could conceivably be stored in cookies, but you wouldn’t want to do that. Cookies are sent to the server with every request, presenting performance issues with large data sets and the potential for security problems, and you have to write your own interface for treating cookies like a database.
Here’s a small demo that stores the content of a textarea in localStorage. You can change the text, open a new tab, and find your updated content. Or you can restart the browser and your text will still be there.
The easiest way to use localStorage is to treat it like a regular object:
>>> localStorage.foo = 'bar' >>> localStorage.foo "bar" >>> localStorage.length 1 >>> localStorage[0] "foo" >>> localStorage['foo'] "bar" >>> delete localStorage['foo'] >>> localStorage.length 0 >>> localStorage.not_set null
There’s also a more wordy API for people who like that sort of thing:
>>> localStorage.clear() >>> localStorage.setItem('foo', 'bar') >>> localStorage.getItem('foo') "bar" >>> localStorage.key(0) "foo" >>> localStorage.removeItem('foo') >>> localStorage.length 0
If you want to have a localStorage database mapped to the current session, you can use sessionStorage. It has the same interface as localStorage, but the lifetime of sessionStorage is limited to the current browser window. You can follow links around the site in the same window and sessionStorage will be maintained (going to different sites is fine too), but once that window is closed the database will be deleted. localStorage is for long-term storage, as the w3c spec instructs browsers to consider the data “potentially user-critical”.
I was a tad disappointed when I found out that localStorage only supports storing strings, since I was hoping for something more structured. But with native JSON support it’s easy to create an object store on top of localStorage:
Storage.prototype.setObject = function(key, value) { this.setItem(key, JSON.stringify(value)); } Storage.prototype.getObject = function(key) { return JSON.parse(this.getItem(key)); }
localStorage databases are scoped to an HTML5 origin, basically the tuple (scheme, host, port). This means that the database is shared across all pages on the same domain, even concurrently by multiple browser tabs. However, a page connecting over http:// cannot see a database that was created during an https:// session.
localStorage and sessionStorage are supported by Firefox 3.5, Safari 4.0, and IE8. You can find more compatibility details on quirksmode.org, including more detail on the storage event.
Yesterday we featured a demo that used SVG to map 3D data. Today we link to Hans’ next demo: dynamically textured animations in the browser.
He uses the same techniques that he used in the previous post, but this time he’s taking an Animated PNG image and mapping a random image texture on top of it using his SVG projection technique. The result is pretty neat. Have a look at the demo:
Firefox 3.5 supports several new CSS3 selectors. In this post we’ll talk about four of them: :nth-child, :nth-last-child, :nth-of-type and :nth-last-of-type.
Each of these is called a Pseudo-class and can be used to apply styles to existing selectors. The best way to describe how this works is with some examples.
This pseudo-class lets you apply styles to groups of elements. The most common use case is to highlight odd or even items in a table:
tr:nth-child(even) { background-color: #E8E8E8; }
A live example (works in Firefox 3.5):
| Row 1 |
| Row 2 |
| Row 3 |
| Row 4 |
But you can also use it to apply styles to groups of more than two using a special notation. The documentation for this rule is pretty obtuse but basically the “3″ in the example splits the number of elements into groups of three and the “+1″ is the offset in that group. There are more examples in the spec as well.
tr:nth-child(3n+1) { background-color: red; } tr:nth-child(3n+2) { background-color: green; } tr:nth-child(3n+3) { background-color: blue; }
A live example (works in Firefox 3.5):
| Row 1 |
| Row 2 |
| Row 3 |
| Row 4 |
| Row 5 |
| Row 6 |
The :nth-last-child pseudo-class works exactly like the :nth-child pseudo-class except that it counts elements in the opposite direction:
tr:nth-last-child(3n+3) { background-color: red; } tr:nth-last-child(3n+2) { background-color: green; } tr:nth-last-child(3n+1) { background-color: blue; }
Example (works in Firefox 3.5):
| Row 1 |
| Row 2 |
| Row 3 |
| Row 4 |
| Row 5 |
| Row 6 |
The :nth-of-type pseudo-class uses the same kind of syntax as the other elements here but allows you to select based on element type.
div:nth-of-type(odd) { border-color: red } div:nth-of-type(even) { border-color: blue }
Example (works in Firefox 3.5):
Much like :nth-last-child, :nth-last-of-type is the same as :nth-of-type except that it counts in the opposite direction.
These four properties allow you to do interesting things with style and element groups and we hope that they make it easier to style your pages.


