My name is Ian Beck. I'm Director of Web Services at Tierra Interactive. Here are things I find useful or interesting.

Posts tagged "javascript"

Tip: if you have a custom array sorting function in Javascript that’s performing right in Firefox but not in Safari, you may need to change what you’re returning. For instance, returning 0 (use same order between first and second element) by default will typically work in Firefox, but for Safari to get the ordering right you’ll need to return a negative number (first item should be sorted ahead) by default.

I have no idea why this is true, but if you’re having issues with Webkit’s Array.sort() functionality that may be the problem. I discovered this trying to sort an array of <img> elements alphabetically by their alt attribute like so (example is using Mootools):

var links = $$('.sometarget img');
links.sort(function(first, second) {
    var firstAlt = first.getElement('img').get('alt');
    var secondAlt = second.getElement('img').get('alt');
    var temp = new Array(firstAlt, secondAlt);
    temp.sort();
    if (temp[0] == firstAlt) {
        return -1;
    } else {
        return 1;
    }
});

Returning 0 worked great in Firefox, but to get it working in Safari, too, I had to switch to returning -1 by default.