Question

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?

Was it helpful?

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top