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 to compare the result the results.

Leave A Reply
All content licensed under the Creative Commons License