Jump to content
Fratozzi

Webp detect JS

Recommended Posts

I am using this function in js to check if the browser supports the webp images format, if it is compatible, release the image in webp, in png. It works with all browsers but if I try it with mobile safari ios 13 it doesn't work I can't understand where is the problem can you help me?

JS


var WebpDetect = function() {

    var onComplete = null;

    var numFeaturesTested = 0;
    var features = [
        { name: 'lossy', testImage: "UklGRiIAAABXRUJQVlA4IBYAAAAwAQCdASoBAAEADsD+JaQAA3AAAAAA" },
        { name: 'lossless', testImage: "UklGRhoAAABXRUJQVlA4TA0AAAAvAAAAEAcQERGIiP4HAA==" },
        { name: 'alpha', testImage: "UklGRkoAAABXRUJQVlA4WAoAAAAQAAAAAAAAAAAAQUxQSAwAAAARBxAR/Q9ERP8DAABWUDggGAAAABQBAJ0BKgEAAQAAAP4AAA3AAP7mtQAAAA==" },
        { name: 'animation', testImage: "UklGRlIAAABXRUJQVlA4WAoAAAASAAAAAAAAAAAAQU5JTQYAAAD/////AABBTk1GJgAAAAAAAAAAAAAAAAAAAGQAAABWUDhMDQAAAC8AAAAQBxAREYiI/gcA" }
    ];

    var testFeatureSupport = function(feature, callback) {

        var image = new Image();

        image.onload = function () {
            var isSupported = image.width > 0 && image.height > 0;
            callback(feature, isSupported);
        };

        image.onerror = function () {
            callback(feature, false);
        };

        image.src = "data:image/webp;base64," + feature.testImage;
    }

    var onFeatureTested = function(feature, isSupported) {
        
        var featureClass = (!isSupported ? 'no-' : '') + 'webp-' + feature.name;
        $('html').addClass(featureClass);

        var featureAttribute = featureClass + '-src';
        $('img[' + featureAttribute + ']').each(function (index, item) {
            var imageSource = $(item).attr(featureAttribute);
            $(item).removeAttr(featureAttribute);

            if (!isSupported)
                imageSource = imageSource.substring(imageSource.length - 3) + 'png';

            $(item).attr('src', imageSource || "");
        });

        if (onComplete) {
            numFeaturesTested++;
            if (numFeaturesTested == features.length) {
                onComplete();
            }
        }
    }

    return {
        init: function (parameters) {
            onComplete = parameters && parameters.onComplete;

            for (var index = 0; index < features.length; ++index) {
                testFeatureSupport(features[index], onFeatureTested);
            }
        }
    }
}();

HTML

<img webp-animation-src="/img/test.webp">

<script src="webp-detect.js" type="text/javascript"></script>

<script>

$('document').ready(function(){

WebpDetect.init();

});

</script>

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.