Javascript 检测 Safari 浏览器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7944460/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Detect Safari browser
提问by Tomas
How to detect Safari browser using JavaScript? I have tried code below and it detects not only Safari but also Chrome browser.
如何使用 JavaScript 检测 Safari 浏览器?我已经尝试过下面的代码,它不仅可以检测 Safari,还可以检测 Chrome 浏览器。
function IsSafari() {
var is_safari = navigator.userAgent.toLowerCase().indexOf('safari/') > -1;
return is_safari;
}
采纳答案by david
You can easily use index of Chrome to filter out Chrome:
您可以轻松地使用 Chrome 的索引来过滤掉 Chrome:
var ua = navigator.userAgent.toLowerCase();
if (ua.indexOf('safari') != -1) {
if (ua.indexOf('chrome') > -1) {
alert("1") // Chrome
} else {
alert("2") // Safari
}
}
回答by fregante
Note:always try to detect the specific behavior you're trying to fix, instead of targeting it with isSafari?
注意:始终尝试检测您要修复的特定行为,而不是针对它isSafari?
As a last resort,detect Safari with this regex:
作为最后的手段,使用此正则表达式检测 Safari:
var isSafari = /^((?!chrome|android).)*safari/i.test(navigator.userAgent);
It uses negative look-aroundsand it excludes Chrome, Edge, and all Android browsers that include the Safari
name in their user agent.
它使用负面环视,不包括 Chrome、Edge 和所有Safari
在其用户代理中包含该名称的Android 浏览器。
回答by qingu
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
如果您仍然想这样做并测试任何 Safari 版本,我建议您使用它
var isSafari = navigator.vendor && navigator.vendor.indexOf('Apple') > -1 &&
navigator.userAgent &&
navigator.userAgent.indexOf('CriOS') == -1 &&
navigator.userAgent.indexOf('FxiOS') == -1;
This will work with any version of Safari across all devices: Mac, iPhone, iPod, iPad.
这适用于所有设备的任何版本的 Safari:Mac、iPhone、iPod、iPad。
Edit
编辑
To test in your current browser: https://jsfiddle.net/j5hgcbm2/
在您当前的浏览器中进行测试:https: //jsfiddle.net/j5hgcbm2/
Edit 2
编辑 2
Updated according to Chrome docsto detect Chrome on iOS correctly
根据Chrome 文档更新以正确检测 iOS 上的 Chrome
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.
值得注意的是,iOS 上的所有浏览器都只是 Safari 的包装器并使用相同的引擎。请参阅 bfred.it 在此线程中对他自己的回答的评论。
Edit 3
编辑 3
Updated according to Firefox docsto detect Firefox on iOS correctly
根据Firefox 文档更新以正确检测 iOS 上的 Firefox
回答by lukyer
Just use:
只需使用:
var isSafari = window.safari !== undefined;
if (isSafari) console.log("Safari, yeah!");
回答by wahid
This code is used to detect only safari browser
此代码仅用于检测 safari 浏览器
if (navigator.userAgent.search("Safari") >= 0 && navigator.userAgent.search("Chrome") < 0)
{
alert("Browser is Safari");
}
回答by tylerlindell
Because userAgent for chrome and safari are nearly the same it can be easier to look at the vendor of the browser
由于 chrome 和 safari 的 userAgent 几乎相同,因此可以更轻松地查看浏览器的供应商
Safari
苹果浏览器
navigator.vendor == "Apple Computer, Inc."
Chrome
铬合金
navigator.vendor == "Google Inc."
FireFox(why is it empty?)
FireFox(为什么是空的?)
navigator.vendor == ""
IE(why is it undefined?)
IE(为什么未定义?)
navigator.vendor == undefined
回答by leoledmag
Only Safariwhitout Chrome:
只有Safari 没有Chrome:
After trying others codes I didn't find any that works with new and old versions of Safari.
在尝试其他代码后,我没有发现任何适用于新旧版本 Safari 的代码。
Finally, I did this code that's working very well for me:
最后,我做了这个对我来说效果很好的代码:
var ua = navigator.userAgent.toLowerCase();
var isSafari = false;
try {
isSafari = /constructor/i.test(window.HTMLElement) || (function (p) { return p.toString() === "[object SafariRemoteNotification]"; })(!window['safari'] || safari.pushNotification);
}
catch(err) {}
isSafari = (isSafari || ((ua.indexOf('safari') != -1)&& (!(ua.indexOf('chrome')!= -1) && (ua.indexOf('version/')!= -1))));
//test
if (isSafari)
{
//Code for Safari Browser (Desktop and Mobile)
document.getElementById('idbody').innerHTML = "This is Safari!";
}
else
{
document.getElementById('idbody').innerHTML = "Not is Safari!";
}
<body id="idbody">
</body>
回答by Christopher Martin
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.
我不知道为什么 OP 想要检测 Safari,但在极少数情况下,您现在需要浏览器嗅探,检测渲染引擎可能比浏览器名称更重要。例如,在 iOS 上,所有浏览器都使用 Safari/Webkit 引擎,因此如果底层渲染器实际上是 Safari/Webkit,那么将“chrome”或“firefox”作为浏览器名称是没有意义的。我没有用旧浏览器测试过这段代码,但它适用于 Android、iOS、OS X、Windows 和 Linux 上的所有最新版本。
<script>
let browserName = "";
if(navigator.vendor.match(/google/i)) {
browserName = 'chrome/blink';
}
else if(navigator.vendor.match(/apple/i)) {
browserName = 'safari/webkit';
}
else if(navigator.userAgent.match(/firefox\//i)) {
browserName = 'firefox/gecko';
}
else if(navigator.userAgent.match(/edge\//i)) {
browserName = 'edge/edgehtml';
}
else if(navigator.userAgent.match(/trident\//i)) {
browserName = 'ie/trident';
}
else
{
browserName = navigator.userAgent + "\n" + navigator.vendor;
}
alert(browserName);
</script>
To clarify:
澄清:
- All browsers under iOS will be reported as "safari/webkit"
- All browsers under Android but Firefox will be reported as "chrome/blink"
- Chrome, Opera, Blisk, Vivaldi etc. will all be reported as "chrome/blink" under Windows, OS X or Linux
- iOS下的所有浏览器都会报告为“safari/webkit”
- Android 下的所有浏览器,但 Firefox 将报告为“chrome/blink”
- Chrome、Opera、Blisk、Vivaldi 等在 Windows、OS X 或 Linux 下都将报告为“chrome/blink”
回答by Piotr Kowalski
I observed that only one word distinguishes Safari - "Version". So this regex will work perfect:
我观察到只有一个词可以区分 Safari - “版本”。所以这个正则表达式将完美运行:
/.*Version.*Safari.*/.test(navigator.userAgent)
回答by lovepong
I use this
我用这个
function getBrowserName() {
var name = "Unknown";
if(navigator.userAgent.indexOf("MSIE")!=-1){
name = "MSIE";
}
else if(navigator.userAgent.indexOf("Firefox")!=-1){
name = "Firefox";
}
else if(navigator.userAgent.indexOf("Opera")!=-1){
name = "Opera";
}
else if(navigator.userAgent.indexOf("Chrome") != -1){
name = "Chrome";
}
else if(navigator.userAgent.indexOf("Safari")!=-1){
name = "Safari";
}
return name;
}
if( getBrowserName() == "Safari" ){
alert("You are using Safari");
}else{
alert("You are surfing on " + getBrowserName(name));
}