Monday, October 11, 2010

Data URI Test

In games, I want to use data URLs for images, but IE only supported this type of URL starting with IE8. Here is some code for testing for URI support. You supply a callback function, and it will be given a true or false value depending on whether data URIs are supported or not (the data URI used is a 1x1 transparent GIF--I couldn't really find anything smaller).

function testDataURLSupport(callbackfn)
{
var img = new Image();
img.onload = function() { callbackfn(true); callbackfn = null; };
img.onerror = function() { callbackfn(false); callbackfn = null; };
img.onabort = function() { callbackfn(false); callbackfn = null; };
img.src = 'data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==';
}

No comments:

Post a Comment