javascript 在javascript中检测webkit浏览器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18625321/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 12:38:52  来源:igfitidea点击:

Detect webkit browser in javascript?

javascriptjquerybrowserwebkit

提问by John Black

EDITED:I actually used PHP to detect and create a local variable with php tags.

编辑:我实际上使用 PHP 来检测和创建一个带有 php 标签的局部变量。

if ( strpos(strtolower($_SERVER['HTTP_USER_AGENT']), 'webkit')) {
    $is_webkit = "true";
}


How do I detect if the browser is webkit based? (Google Chrome, newer Opera, safari);

如何检测浏览器是否基于 webkit?(谷歌浏览器、较新的 Opera、Safari);

I've tried this:

我试过这个:

var isWebkit = (window.webkitURL != null);
if(isWebkit){
    alert("i am a webkit based browser!");
}

Doesn't work for safari

不适用于 safari

采纳答案by Afzaal Ahmad Zeeshan

The basic of them all is this: w3school JS.

它们的基本原理是:w3school JS

Code:

代码:

<script>  
txt = "<p>Browser CodeName: " + navigator.appCodeName + "</p>";  
txt+= "<p>Browser Name: " + navigator.appName + "</p>";  
txt+= "<p>Browser Version: " + navigator.appVersion + "</p>";  
txt+= "<p>Cookies Enabled: " + navigator.cookieEnabled + "</p>";  
txt+= "<p>Platform: " + navigator.platform + "</p>";  
txt+= "<p>User-agent header: " + navigator.userAgent + "</p>";  
txt+= "<p>User-agent language: " + navigator.systemLanguage + "</p>";  
document.getElementById("example").innerHTML=txt;  
</script>

But this is not sure about -webkit-.

但这并不确定-webkit-。

Here is a fiddle for that, I mean a fiddle for the -webkit- browser alert! (For Chrome, Safari only; Opera 15+ not supported yet!) jsfiddle.

这是一个小提琴,我的意思是 -webkit- 浏览器警报的小提琴!(仅适用于 Chrome、Safari;尚不支持 Opera 15+!)jsfiddle。

Here is a jQuery code, for this! try this:

这是一个 jQuery 代码,为此!试试这个:

$(document).ready(function(){

    /* Get browser */
    $.browser.chrome = /chrome/.test(navigator.userAgent.toLowerCase());

    /* Detect Chrome */
    if($.browser.chrome){
        /* Do something for Chrome at this point */
        /* Finally, if it is Chrome then jQuery thinks it's 
           Safari so we have to tell it isn't */
        $.browser.safari = false;
    }

    /* Detect Safari */
    if($.browser.safari){
        /* Do something for Safari */
    }

});

This will show a popup, as soon as the windows loads!

一旦窗口加载,这将显示一个弹出窗口!

The best and easy and readable solution would be this:

最好的、简单易读的解决方案是这样的:

$.browser.chrome = $.browser.webkit && !!window.chrome;  
$.browser.safari = $.browser.webkit && !window.chrome;  
if ($.browser.chrome) alert("You are using Chrome!");  
if ($.browser.safari) alert("You are using Safari!");

These were the basics, that I found on some sites:

这些是我在一些网站上找到的基础知识:

  1. w3schools.com
  2. stackoverflow(I used this site to find fiddles.
  1. w3schools.com
  2. stackoverflow(我使用这个站点来查找小提琴。

回答by VoVaVc

The solution is pretty simple

解决方法很简单

if(navigator.userAgent.indexOf('AppleWebKit') != -1){
    //this is webkit!
}

AppleWebKit is the name of webkit rendering engine, so every single webkit-based browser must contain this string

AppleWebKit 是 webkit 渲染引擎的名称,所以每个基于 webkit 的浏览器都必须包含这个字符串

MDN page about browser detection & user agent

关于浏览器检测和用户代理的 MDN 页面

but if you need trustable information about user's browser engine, you might want to use this:

但如果您需要有关用户浏览器引擎的可信信息,您可能需要使用以下方法:

if(typeof window.webkitConvertPointFromNodeToPage === 'function'){
    // this is webkit (really it is)
}

Explanation: userAgent property can be easily spoofed, so checking specific property is a much better way. But it is not defectless, early versions of safari (before 4.0) doesn't have such property. Chrome supports webkitConvertPointFromNodeToPage since first version, opera supports this as well as it's engine transferred to webkit

说明:userAgent 属性很容易被欺骗,因此检查特定属性是一种更好的方法。但它并非完美无缺,safari 的早期版本(4.0 之前)没有这样的属性。Chrome 从第一个版本开始就支持 webkitConvertPointFromNodeToPage,opera 支持这个以及它的引擎转移到 webkit

回答by ling

alert('webkitRequestAnimationFrame' in window);

Tested consistently in Chrome 48 (true), Safari 9.0.2 (true), Opera 35 (true), Firefox 44.0.2 (false)

在 Chrome 48 (true)、Safari 9.0.2 (true)、Opera 35 (true)、Firefox 44.0.2 (false) 中一致测试

回答by Jeffpowrs

From this Post:How to detect chrome and safari browser (webkit)

来自这篇文章:如何检测 chrome 和 safari 浏览器 (webkit)

var isChrome = /Chrome/.test(navigator.userAgent) && /Google Inc/.test(navigator.vendor);
var isSafari = /Safari/.test(navigator.userAgent) && /Apple Computer/.test(navigator.vendor);

if (isChrome) alert("You are using Chrome!");
if (isSafari) alert("You are using Safari!");

Or more general webkit:

或者更通用的 webkit:

var isWebkit = /webkit/.test( navigator.userAgent );

You should really be doing feature detection using something like Modernizr.

您真的应该使用 Modernizr 之类的工具进行特征检测。

回答by Yaron Meiner

In order to know if it's webkit in general:

为了知道它是否是一般的 webkit:

isWebkit = /(safari|chrome)/.test(navigator.userAgent.toLowerCase());

The word safari might appear both in chrome and safari.

safari 一词可能同时出现在 chrome 和 safari 中。