Detection for Safari Windows with Javascript

2020-07-17 15:10发布

问题:

How do I check if user is using Safari on the Windows? Because I need to apply some special styles to the site.

回答1:

You can test navigator.userAgent for the strings Safari/ and Windows (list of Safari user agent strings), e.g.: Live Example

var ua = navigator.userAgent.toLowerCase();
if (ua.indexOf("safari/") !== -1 &&  // It says it's Safari
    ua.indexOf("windows") !== -1 &&  // It says it's on Windows
    ua.indexOf("chrom")   === -1     // It DOESN'T say it's Chrome/Chromium
    ) {
    // Looks like Safari on Windows (but browser detection is unreliable and best avoided)
}

...but browser detection is usually not the best solution to a problem. Much better, where you can, to use feature detection. You can feature-detect nearly everything. Kangax has a great list of feature detects, and of course libs like Moderizr do them as well. But I don't know your use-case...



回答2:

This plugin seems to be exactly what you want. Plugin Link

On the plugin page they link to a test page that runs the plugin which will shoot back the browser and OS. Test Link