All entries from Apr 2008

25 Apr 2008
21 Apr 2008

Prototype, jQuery and the toggling of hidden elements

Just wanted to share a couple of little Prototype niggles. In general I'm pro-Prototype because it feels like it's a closer fit to the typical ways of working that you see in non-library-using Javascript. JQuery, on the other hand, has chaining and acts on elements using the same interfaces regardless of whether there are one or many. From the dabbling I've done, it seems like that gets you into the odd head-twisting situation, but I've yet to embark on a big project with jQuery, so judgement is reserved for now.

Having said that, here's one instance where jQuery comes out on top. Showing & hiding of elements is a pretty common JS/DOM task and you're probably familiar with an issue where hidden elements need an inline style of style="display:none;" to allow them to be shown again. This is because the un-hiding is done by setting the display property of the element to an empty string, thereby allowing the element to re-inherit it's default style. If the styling is done via CSS instead (.hide { display:none; }) then the styling is further up the inheritance chain so the element always inherits from here instead and can't be unhidden.

Prototype has the most basic show/hide implementation and makes no attempt at a work around. Its documentation even makes specific reference to the issue: "Element.show cannot display elements hidden via CSS stylesheets. Note that this is not a Prototype limitation but a consequence of how the CSS display property works".

Here's the implementation (from version 1.6; current at time of writing):

Element.Methods = {
    hide: function(element) {
        $(element).style.display = 'none';
        return element;
    },
    show: function(element) {
        $(element).style.display = '';
        return element;
    }
}

But that's not the end of the story. JQuery does manage a workaround, and an effective one at that. When failing to show a hidden element it adds a temporary, unadorned element of the appropriate type to the document and checks what its display property is set to by default. Finally, if all else fails it resorts to "block".

Here's a version of the jQuery methods, ported for use with Prototype (the originals begin at line 2890 in jquery-1.2.3.js):

Element.addMethods({
    show: function(elem){
        elem.style.display = elem.oldDisplay || '';
        if (elem.getStyle('display') == 'none') {
            var test = document.createElement(elem.tagName);
            $(document.body).insert({bottom: test});
            elem.style.display = test.getStyle('display');
            if (elem.style.display == 'none')
                elem.style.display = 'block';
            test.remove();
        }
        return elem;
    },
    hide: function(elem){
        elem.oldDisplay = elem.oldDisplay || elem.getStyle('display');
        elem.setStyle({display: 'none'});
        return elem;
    }
});

Much nicer. I'm not sure what the guiding philosophy is for what gets included and what's left out of the big JS libraries. It seems like something like this should be in there as an easy fix to a common problem. I would think that the identity of the libraries lies more in their syntax and aesthetics than their capabilities, so there's little to lose from liberal cross-pollenation.

0 comments

19 Apr 2008
15 Apr 2008
14 Apr 2008

A new dimension in film-making

I haven't read too deeply into this, but I've seen a couple of signs lately that the movie industry might be looking to make money out of 3D (as in 3D glasses, and not specifically CGI). Disney's re-releasing the Toy Story films in 3D, Pixar is planning on releasing all it's films in 3D starting next year, and James Cameron has been doing some serious research into it for his closely-guarded upcoming blockbuster "Avatar". I wonder if this is something they're banking on to differentiate the big-screen experience from the DVD market. A return to the days when a trip to the cinema was a big event.

Not that home entertainment necessarily has to miss out, there's also the question of whether the videogame industry might be quick to make a similar move. Apparently BoomBlox (the upcoming EA / Steven Speilberg collaboration for the Wii) is to feature a head-tracking 3D mode as an easter egg. See Johnny Lee's presentation at TED for a recent demo of this technique.

2 comments

9 Apr 2008
5 Apr 2008
4 Apr 2008

Other dates

Visit the archive if you wish to skip merrily through time, tumbling this way and that at your leisure.