I am using this popular code from StackOverflow, and it works fine in terms of setting an image.src to this base64 URL, but I can't see the actual URL for some reason. I put arrows to point where my errors are. This is the weird part though: when I have an img element on my HTML page and I set the img.src property to the dataURL created from this method, it works perfectly. How do I extract and see the string?
// Code taken from MatthewCrumley (http://stackoverflow.com/a/934925/298479)
function getBase64Image(img) {
// Create an empty canvas element
var canvas = document.createElement("canvas");
canvas.width = img.width;
canvas.height = img.height;
// Copy the image contents to the canvas
var ctx = canvas.getContext("2d");
// alert(canvas.toDataURL("image/png"); <-- THIS PRINTS A NORMAL DATA URL FINE
ctx.drawImage(img, 0, 0);
// alert(canvas.toDataURL("image/png"); <-- THIS PRINTS AN EMPTY STRING (BLANK ALERT PAGE)
// Get the data-URL formatted image
// Firefox supports PNG and JPEG. You could check img.src to guess the
// original format, but be aware the using "image/jpg" will re-encode the image.
var dataURL = canvas.toDataURL("image/png");
return dataURL.replace(/^data:image\/(png|jpg);base64,/, "");
}