I have a bit of an issue with a function running in chrome that works properly in Safari, both webkit browsers...
I need to customize a variable in a function for Chrome, but not for Safari.
Sadly, I have been using this to detect if it is a webkit browser:
if ($.browser.webkit) {
But I need to detect:
if ($.browser.chrome) {
Is there any way to write a similar statement (a working version of the one above)?
This question was already discussed here: JavaScript: How to find out if the user browser is Chrome?
Please try this:
But a more complete and accurate answer would be this since IE11, IE Edge, and Opera will also return
true
forwindow.chrome
So use the below:
Above posts advise to use jQuery.browser. But the jQuery API recommends against using this method.. (see DOCS in API). And states its functionality may be moved to a team-supported plugin in a future release of jQuery.
The jQuery API recommends to use
jQuery.support
.The reason being is that 'jQuery.browser' uses the user agent which can be spoofed and it is actually deprecated in later versions of jQuery. If you really want to use $.browser.. Here is the link to the standalone jQuery plugin, since it has been removed from jQuery version 1.9.1. https://github.com/gabceb/jquery-browser-plugin
It's better to use feature object detection instead of browser detection.
Also if you use the Google Chrome inspector and go to the console tab. Type 'window' and press enter. Then you be able to view the DOM properties for the 'window object'. When you collapse the object you can view all the properties, including the 'chrome' property.
I hope this helps, even though the question was how to do with with jQuery. But sometimes straight javascript is more simple!
userAgent can be changed. for more robust, use the global variable specified by chrome
UPDATE:(10x to @Mr. Bacciagalupe)
jQuery has removed
$.browser
from 1.9 and their latest release.But you can still use $.browser as a standalone plugin, found here
When I test the answer @IE, I got always "true". The better way is this which works also @IE:
As described in this answer: https://stackoverflow.com/a/4565120/1201725
As a quick addition, and I'm surprised nobody has thought of this, you could use the
in
operator:Obviously this isn't using JQuery, but I figured I'd put it since it's handy for times when you aren't using any external libraries.