There is a viable way to support image detection for an image: data base64

using something line by line:

background:url(data:image/gif;base64,R0lGODlhIwAhALMAAAAAADQ0NENDQ25ubouLi6ioqLa2ttPT0/Dw8P///wAAAAAAAAAAAAAAAAAAAAAAACwAAAAAIwAhAAAIpQABCBxIsCCAAAYTKlw4cECCAQwjMnSY4KHEiwQpVrSIUaLGjRw7Kvy4EYEAkSNBljyJ0iDJiiZbulQJk6XMhjQTxLyJk+ZOngBe6rTJU+jPojmTKqXZc6nTpAKFPp0qsMDUqyoHWsWKleBWrk8LfgV5AKjYnGXNakWrdi3NtG3HbjTQtmrOAnUByK2It+7eBH3j5iSQVy5cv3PzegWsuCDExmYDAgA7) no-repeat center center;}

excellent, but I want to provide a graceful degradation (css statement via javascript) when base64 is unavailable.

it is obvious that IE prior to v8 does not have this functionality, so I could go as a browser - but I would prefer the function to be detected if possible.

any ideas on how to do this?

+5
source share
3 answers

mootools Browser.Features( - , modernizr )

http://www.jsfiddle.net/dimitar/5JT45/13/show/ https://gist.github.com/821370

(function() {
    Browser.Features.base64 = null;
    var callback = function() {
        Browser.Features.base64 = this.width == 1 && this.height == 1;        
        // alert(Browser.Features.base64); // true || false
    };

    var img = new Image(), img = document.id(img) || new Element("img");
    img.onload = img.onerror = img.onabort = callback;
    // 1x1 px gif to test with
    img.src = "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==";

})();
+2

Modernizr https://github.com/Modernizr/Modernizr/issues/14:

Modernizr.addTest('datauri',function(){
  var data = new Image();
  data.onload = data.onerror = function(){
    return (this.width == 1 && this.height == 1);
  }
  data.src = "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==";
})
+1

All Articles