javascript: make base64 encoded string url friendly








based64 encoded string contains some characters that are not valid to be urls. The following javascript functions can be used to make a url safe string from a base64 encoded string.
function encodeURL(str){
    return str.replace(/\+/g, '-').replace(/\//g, '_').replace(/\=+$/, '');
}

function decodeUrl(str){
    str = (str + '===').slice(0, str.length + (str.length % 4));
    return str.replace(/-/g, '+').replace(/_/g, '/');
}

See also




1 comment: