检测 Safari 浏览器
如何使用 JavaScript 检测 Safari 浏览器?我尝试过下面的代码,它不仅可以检测 Safari,还可以检测 Chrome 浏览器。
function IsSafari() {
var is_safari = navigator.userAgent.toLowerCase().indexOf('safari/') > -1;
return is_safari;
}
How to detect Safari browser using JavaScript? I have tried code below and it detects not only Safari but also Chrome browser.
function IsSafari() {
var is_safari = navigator.userAgent.toLowerCase().indexOf('safari/') > -1;
return is_safari;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(25)
我测试了 #Christopher Martin 发布的代码,它将我的浏览器报告为 Chrome,因为它在测试 Edge 之前对此进行了测试,否则 Edge 会回答用于识别 Chrome 的测试。我修改了他的答案以纠正这个缺陷和另外两个缺陷,即:
将代码转换为函数会产生以下函数和测试脚本,通过调试控制台进行报告。
I tested the code posted by #Christopher Martin, and it reported my browser as Chrome, because it tests for that before testing for Edge, which would otherwise answer true to the test that is intended to identify Chrome. I amended his answer to correct that deficiency and two others, namely:
Converting the code into a function yields the following function and test script that reports via the debug console.
基于@SudarP 的回答。
到 2021 年第 3 季度,此解决方案将在 Firefox (
Uncaught TypeError: navigator.vendor.match(...) is null
) 和 Chrome (Uncaught TypeError: Cannot read properties of null (reading)) 中失败'长度')
);所以这是一个固定且更短的解决方案:
Based on @SudarP answer.
At Q3 2021 this solution will fail in either Firefox (
Uncaught TypeError: navigator.vendor.match(...) is null
) and Chrome (Uncaught TypeError: Cannot read properties of null (reading 'length')
);So here is a fixed and shorter solution:
此代码仅用于检测safari浏览器
This code is used to detect only safari browser
我创建一个返回布尔类型的函数:
I create a function that return boolean type:
用户代理嗅探确实很棘手且不可靠。我们试图用类似上面 @qingu 的答案来检测 iOS 上的 Safari,它对于 Safari、Chrome 和 Firefox 确实工作得很好。但它错误地将 Opera 和 Edge 检测为 Safari。
因此我们采用了功能检测,从目前来看,serviceWorker 仅在 Safari 中受支持,iOS 上的任何其他浏览器均不支持。如 https://jakearchibald.github.io/isserviceworkerready/ 中所述
所以我们做了类似
注意的事情:未在 MacOS 上的 Safari 上进行测试。
User agent sniffing is really tricky and unreliable. We were trying to detect Safari on iOS with something like @qingu's answer above, it did work pretty well for Safari, Chrome and Firefox. But it falsely detected Opera and Edge as Safari.
So we went with feature detection, as it looks like as of today,
serviceWorker
is only supported in Safari and not in any other browser on iOS. As stated in https://jakearchibald.github.io/isserviceworkerready/So we did something like
Note: Not tested on Safari on MacOS.
注意:始终尝试检测您要修复的特定行为,而不是使用
isSafari?
作为目标,作为最后的手段,检测具有此正则表达式的 Safari:
它使用负查找,并且排除 Chrome、Edge 以及所有包含 <代码>Safari他们的用户代理中的名称。
Note: always try to detect the specific behavior you're trying to fix, instead of targeting it with
isSafari?
As a last resort, detect Safari with this regex:
It uses negative look-arounds and it excludes Chrome, Edge, and all Android browsers that include the
Safari
name in their user agent.正如其他人已经指出的那样,功能检测优于检查特定浏览器。原因之一是用户代理字符串可以更改。另一个原因是该字符串可能会更改并破坏较新版本中的代码。
如果您仍然想这样做并测试任何 Safari 版本,我建议使用此方法,
这适用于所有设备上的任何版本的 Safari:Mac、iPhone、iPod、iPad。
编辑
要在当前浏览器中进行测试: https://jsfiddle.net/j5hgcbm2/
编辑 2
根据 < a href="https://developer.chrome.com/multidevice/user-agent" rel="noreferrer">Chrome 文档,用于正确检测 iOS 上的 Chrome
值得注意的是,iOS 上的所有浏览器都是只是 Safari 的包装并使用相同的引擎。请参阅 bfred.it 在该线程中对他自己的答案的评论。
编辑3
根据Firefox 文档更新正确检测 iOS 上的 Firefox
As other people have already noted, feature detection is preferred over checking for a specific browser. One reason is that the user agent string can be altered. Another reason is that the string may change and break your code in newer versions.
If you still want to do it and test for any Safari version, I'd suggest using this
This will work with any version of Safari across all devices: Mac, iPhone, iPod, iPad.
Edit
To test in your current browser: https://jsfiddle.net/j5hgcbm2/
Edit 2
Updated according to Chrome docs to detect Chrome on iOS correctly
It's worth noting that all Browsers on iOS are just wrappers for Safari and use the same engine. See bfred.it's comment on his own answer in this thread.
Edit 3
Updated according to Firefox docs to detect Firefox on iOS correctly
只需使用:
请注意,这对于移动版本的 Safari 可能不可靠。
Just use:
Note this might not be reliable for mobile versions of Safari.
您可以轻松地使用 Chrome 的索引来过滤掉 Chrome:
You can easily use index of Chrome to filter out Chrome:
此代码仅用于检测safari浏览器
This code is used to detect only safari browser
因为 chrome 和 safari 的 userAgent 几乎相同,所以可以更容易地查看浏览器的供应商
Safari
Chrome
FireFox (为什么是这样空?)
IE(为什么它是未定义的?)
Because userAgent for chrome and safari are nearly the same it can be easier to look at the vendor of the browser
Safari
Chrome
FireFox (why is it empty?)
IE (why is it undefined?)
阅读许多答案和帖子并确定最准确的解决方案。在 Safari、Chrome、Firefox 和 Firefox 中进行了测试Opera(桌面版和 iOS 版)。首先我们需要检测
Apple
供应商,然后排除 Chrome、Firefox 和 Firefox。 Opera(适用于 iOS)。Read many answers and posts and determined the most accurate solution. Tested in Safari, Chrome, Firefox & Opera (desktop and iOS versions). First we need to detect
Apple
vendor and then exclude Chrome, Firefox & Opera (for iOS).检测手势更改支持,如下所示:
漂亮又简单;适用于 iOS 和 Android!如果您需要 ES5 支持,请将
const
替换为var
。请注意,如果人们使用 WKWebView 进行针对 Apple 设备的应用程序开发,它可能会声称它是 Safari,这在技术上是正确的,因为它在后台使用 Safari 的网络功能。可能需要其他解决方案,例如注入额外的代码。
Detect gesture change support, like this:
Nice and simple; works for iOS and Android! If you need ES5 support, replace
const
withvar
.Note that if people are using WKWebView for app development tailored to an Apple device, it may claim it is Safari, which is technically true, given that it uses Safari's web features in the background. Other solutions may be necessary, such as injecting additional code.
我不知道为什么OP想要检测Safari,但在极少数情况下,您现在需要浏览器嗅探,检测渲染引擎可能比检测浏览器名称更重要。例如,在 iOS 上,所有浏览器都使用 Safari/Webkit 引擎,因此如果底层渲染器实际上是 Safari/Webkit,那么使用“chrome”或“firefox”作为浏览器名称是没有意义的。我还没有在旧浏览器上测试过这段代码,但它适用于 Android、iOS、OS X、Windows 和 Linux 上的所有最新版本。
澄清一下:
I don't know why the OP wanted to detect Safari, but in the rare case you need browser sniffing nowadays it's problably more important to detect the render engine than the name of the browser. For example on iOS all browsers use the Safari/Webkit engine, so it's pointless to get "chrome" or "firefox" as browser name if the underlying renderer is in fact Safari/Webkit. I haven't tested this code with old browsers but it works with everything fairly recent on Android, iOS, OS X, Windows and Linux.
To clarify:
就我而言,我需要在 iOS 和 macOS 上定位 Safari。这对我有用:
In my case I needed to target Safari on both iOS and macOS. This worked for me:
只有 Safari 没有 Chrome:
在尝试其他代码后,我没有找到任何适用于新旧版本 Safari 的代码。
最后,我编写了这段对我来说非常有效的代码:
Only Safari whitout Chrome:
After trying other's code I didn't find any that works with new and old versions of Safari.
Finally, I did this code that's working very well for me:
我发现只有一个词可以区分 Safari:“版本”。所以这个正则表达式将完美工作:
I observed that only one word distinguishes Safari - "Version". So this regex will work perfect:
我用这个
I use this
最简单的答案:
Simplest answer:
修改了答案的正则表达式上述
Modified regex for answer above
我知道这个问题已经很老了,但我还是想发布答案,因为它可能会对某人有所帮助。上述解决方案在某些边缘情况下会失败,因此我们必须以分别处理 iOS、桌面和其他平台的方式来实现它。
I know this question is old, but I thought of posting the answer anyway as it may help someone. The above solutions were failing in some edge cases, so we had to implement it in a way that handles iOS, Desktop, and other platforms separately.
2023年版
2023 edition
2024 版本
2024 Version
根据记录,我发现的最安全的方法是实现此答案中的浏览器检测代码的 Safari 部分:
当然,如果可能的话,处理特定于浏览器的问题的最佳方法始终是进行功能检测。不过,使用像上面这样的一段代码仍然比代理字符串检测更好。
For the records, the safest way I've found is to implement the Safari part of the browser-detection code from this answer:
Of course, the best way of dealing with browser-specific issues is always to do feature-detection, if at all possible. Using a piece of code like the above one is, though, still better than agent string detection.
这个独特的“问题”100% 表明浏览器是 Safari(不管你信不信)。
这意味着 Cookie 对象描述符在 Safari 上设置为 false,而在其他所有浏览器上设置为 true,这实际上是让我对另一个项目感到头疼。快乐编码!
This unique "issue" is 100% sign that browser is Safari (believe it or not).
This means that cookie object descriptor is set to false on Safari while on the all other is true, which is actually giving me a headache on the other project. Happy coding!