Search code examples
safarisafari-extensionsafari6

Can I detect if Safari is version 6 from within a Safari Extension?


Basically I have a Safari Extension and I want to deliver slightly different code to Safari 6 from that that I use for Safari 5.1...

Is there a way I can detect the browser version from within an extension?


Solution

  • If your Safari 6-specific code depends on a new API in Safari 6, you should use feature detection to determine whether or not you can use the feature. For example, if you wanted to address Safari 6's new Reader API:

    var thisTab = safari.application.activeBrowserWindow.activeTab;
    
    if (thisTab.reader) {
        // do something with Reader
    } else {
        // do something else
    }
    

    If feature detection won't work for you, you can use window.navigator.appVersion to determine the major version of Safari. For example:

    var majorVersion = /Version\/(\d+\.?\d*)/.exec(navigator.appVersion)[1];
    

    But feature detection is always preferable.