最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Detect webkit browser in javascript? - Stack Overflow

programmeradmin2浏览0评论

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

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);

I've tried this:

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

Doesn't work for safari

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

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);

I've tried this:

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

Doesn't work for safari

Share Improve this question edited Sep 4, 2013 at 23:39 John Black asked Sep 4, 2013 at 23:17 John BlackJohn Black 3053 gold badges8 silver badges19 bronze badges 3
  • 1 YUNO do feature detection instead of engine sniffing? – Fabrício Matté Commented Sep 4, 2013 at 23:18
  • Look here: stackoverflow.com/questions/12625876/… – sushain97 Commented Sep 4, 2013 at 23:18
  • you want to inspect navigator.userAgent – ama2 Commented Sep 4, 2013 at 23:19
Add a comment  | 

5 Answers 5

Reset to default 13

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

MDN page about browser detection & user agent

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

alert('webkitRequestAnimationFrame' in window);

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

The basic of them all is this: 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-.

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.

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

$(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.

From this Post: How to detect chrome and safari browser (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:

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

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

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

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

The word safari might appear both in chrome and safari.

发布评论

评论列表(0)

  1. 暂无评论