AJAX - The Complete Reference

(avery) #1

Chapter 4: Data Formats 171


PART I


if (dec3 != 64)
outputStr += String.fromCharCode(byte2);
if (dec4 != 64)
outputStr += String.fromCharCode(byte3);
}
return outputStr;
}

The live example can be found at http://ajaxref.com/ch4/base64response.html.

Experimenting with Data URLs

If you take the ideas from the previous section, you might see an interesting possibility to
use image data with XHRs using an addressing scheme called a data: URI (http://tools
.ietf.org/html/rfc2397). A data: URI allows you to include data directly in the address as
an immediate form of information ready for consumption without another network fetch.
The format of the URI is

data: [Mime type] [;base64],data

For example, if you used the following data: URI in a browser that can handle the scheme
such as Firefox, Opera, or Safari:

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAFwAAAAlCAIAAABAoBkGAAAAAXN
SR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAgY0hSTQAAeiYAAICEAAD6AAAAgOgAAHUwAADq
YAAAOpgAABdwnLpRPAAAAbNJREFUaEPtmOFug0AMg9mb783ZJiqU2U7wAdUGpNqPqoNc7sN2T
nzM8zz1Bwh8Q+kPEJiaCBNoKEIVDaWheGHRSmmltFI8Aq0Uj1OaKdPntP7FUsuPXnH3qriWX
NQpNHRjffH/hTKK/ilQhri8HQrr2RHw7mukfXb41L9lj33W6kP4HwoFGHEYwy8+06zyArqI/
GixqJS61TOVEmtJi70DCiy06jF7JMyRK9wTiiSVKeXyUGpLrmb5Myjc32rv+MX8XsdwMe8g9aM
7zKW5Qj2nxOGN+2NZRmNDZzEX67X5oAxCcPYve4N1M6zZc0qhcJ5Bi5BnPAV8IjxfNrcqi2eRL
OdXbBjoHIIinwAoxTy8wDiAyssGTFLS9VLO5ysl81SRDgWgE6Gwoo/aBzCzIOvYK5LPIcICib
BMpZhQqn7QTr/fGGxCKVI5kzF3k60ii3OWRXNBVMO/pCtFPw2lobzekEGEbUwfP1P4PFLcW88
gOUo5QWCIymzetA+4TzY2/GIx9mqO28td1lDEIxuDUlvxcooYOLw5B4rb7P+ETHlCoPwMkHs/
8327ayiHg3Yf+Mvd9QXKND53ovXqRQAAAABJRU5ErkJggg==

you would see an image stating an obvious fact felt by the author.

Obviously, you can use this format to directly embed an image using the <img> tag like so:

<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAFwAAAAlCAIAAABAoBk
GAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAgY0hSTQAAeiYAAICEAAD6AAAAgOgAAH
UwAADqYAAAOpgAABdwnLpRPAAAAbNJREFUaEPtmOFug0AMg9mb783ZJiqU2U7wAdUGpNqPq-
Free download pdf