Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect webkit browser in javascript?

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

like image 823
John Black Avatar asked Nov 30 '22 20:11

John Black


1 Answers

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

like image 89
VoVaVc Avatar answered Dec 03 '22 08:12

VoVaVc