Tagged: javascript

SWFUpload with flash 10

Flash player version 10 has new security restrictions with it’s javascript interaction.  It is no longer possible to trigger the local file picker from a javascript event.  Basically breaking the most legitimate use of flash in websites that I’ve seen in a while.

I use SWFUpload in a few sites to get multiple file uploads and upload progess bars.  It’s pretty slick.

The SWFUpload project has a beta release out that can work with Flash 10.  Here’s the basic changes required in your code to work with the 2.2.0 betas:

Right above your html that contains a button to trigger the file picker:

You will want to give your button a fixed width and height.

Add the following CSS rules:

.swfupload {
    position: absolute;
    z-index: 1;
}

And finally, add the following options to your SWFUpload settings object:

     button_placeholder_id : "spanButtonPlaceholder",
    button_width: 100,
    button_height: 40,
    button_window_mode: SWFUpload.WINDOW_MODE.TRANSPARENT,
    button_cursor: SWFUpload.CURSOR.HAND,

This adds a transparent flash object over your html button that is the same size. Flash traps the click over your button and triggers the file picker from flash itself. Ironically, this work around for the tighter security restrictions in Flash 10 seems like a compete security risk in inself…

Pat wrote about fixing the flash 10 issue with the YUI libraries.

Greasemonkey script for twitter

A few weeks ago I was thinking about how hilarious it would be to view twitter pages with OMG: prepended to all the messages. I’ve had a post-it note attached to my monitor ever since that has 3 words: twitter greasemonkey omg.

I present my first greasemonkey script: twitter omg.

To be honest, I’ve never actually installed greasemonkey before. Writing the script was quite fun. I’ll be looking for opportunities to make a script that is actually useful…

I love code snippet challenges

I stumbled upon a blog with a random code snippet challenge. Here was the problem at hand:

Given a String (x) containing only characters a-z, write a function (f) that returns a base 10 integer, which converts the String as if it were a base 26 numeral. Function f is bijective.

I wanted to give it a stab in javascript not because it sounded particularly difficult but to play with different javascript code styles. Here’s my 4 versions:

function f(i) {

    if (!i) { return 0; }

    var parts, n, pow, val;
    parts = i.split('');
    parts.reverse();

    val = 0;
    pow = 1;
    for (n = 0; n < parts.length; ++n) {
        val += pow * (parts[n].charCodeAt(0) - 96);
        pow = pow * 26;
    }

    return val;
}

The first one turns the string into an array and reverses it before doing the math loop. My next try eliminates the array and iterates backwards over the string.

function f(i) {

    if (!i) { return 0; }

    var n, mult = 1, val = 0, offset = 96, base = 26;

    for (n = i.length - 1; n > -1; n--) {
        val += mult * (i.charCodeAt(n) - offset);
        mult = mult * base;
    }

    return val;

}

Here I’m playing with chaining array functions. While I don’t create an array, this is very similar to the first try.

function f(i) {

    if (!i) { return 0; }

    var mult = 1, val = 0, offset = 96, base = 26;

    i.split('').reverse().forEach(function(el) {
        val += mult * (el.charCodeAt(0) - offset);
        mult = mult * base;
    });   

    return val;

}

Finally, recursion instead of an iterative loop.

function f(i, mult) {

    if (!i) { return 0; }
    if (!mult) { mult = 1; }

    var offset = 96, base = 26;
    return mult * (i.charCodeAt(i.length - 1) - offset)
             + f(i.substr(0, i.length - 1), mult * base);

}

Checkout the test output of my functions. Works in Firefox 1.5+ or other browsers that implement more modern javascript functions. Shouldn’t work in IE.

Written by Comments Off on I love code snippet challenges Posted in General Tagged with