Search code examples
javascriptjquerybrowser-detectionzepto

Zepto fallback to jQuery


I'm using ZeptoJS for my web app, but I'd like to fall back to jQuery if the browser doesn't support Zepto. Since IE is the only major browser not supported at the moment, I'm tempted to detect IE:

if(navigator.appName == 'Microsoft Internet Explorer'){
    // load jquery
} else {
    // load zepto
}

but I'd prefer to specificly detect Zepto support and use jQuery in other cases. Is there a feature detection way to do this?


Solution

  • This might be a crazy idea (I'm not sure if Zepto will even load on an unsupported browser), but what about using Zepto's own browser detection to see if it's on an unsupported browser?

    $.os.ios      // => true if running on Apple iOS
    $.os.android  // => true if running on Android
    $.os.webos    // => true if running on HP/Palm WebOS
    $.os.touchpad // => true if running on a HP TouchPad
    $.os.version  // => string with version number, "4.0", "3.1.1", "2.1", etc.
    $.os.iphone   // => true if running on iPhone
    $.os.ipad     // => true if running on iPad
    $.os.blackberry // => true if running on BlackBerry
    

    Maybe you could do something like this:

    var isSupported = false;
    for (os in $.os) {
        if ($.os[os] == true) { isSupported = true; }
    }
    

    This won't catch chrome/firefox, which work fine with Zepto, but it does match the Zepto team's intentions for the thing, which may or may not be better.