Browser base64 support

Firefox, Chrome, Safari, and Opera have native support for base64 encoding in javascript in the form of the btoa() function. Unfortunately Internet Explorer does not support this, so it's not safe to rely on. There are many base64 javascript libraries you can use though, the performance is just lower. You can do a hybrid solution however, where the browser will use the native btoa() function if it is available, and if not fall back to the javascript implementation. This uses the javascript typeof() function to determine if the native btoa() function is available:

function hybrid_encode(txt) {
    if (typeof(btoa) === 'function') {
        return btoa(txt);
    } else {
        return Base64.encode(txt);
    }
}

I wrote up a test case on jsperf.com to compare the result the results.



Note: Replies will be formatted with PHP Markdown Extra syntax.

Name: Email (Not Required):
 
Logged IP: 3.17.28.48
To prevent spam please submit by clicking the kitten: