5

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

3
  • 1
    YUNO do feature detection instead of engine sniffing? Commented Sep 4, 2013 at 23:18
  • Look here: stackoverflow.com/questions/12625876/… Commented Sep 4, 2013 at 23:18
  • you want to inspect navigator.userAgent Commented Sep 4, 2013 at 23:19

5 Answers 5

14

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

Sign up to request clarification or add additional context in comments.

1 Comment

window.webkitConvertPointFromNodeToPage is deprecated.
5
alert('webkitRequestAnimationFrame' in window);

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

Comments

1

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.

Comments

1

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.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.