Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript : Execute code only if it is not google chrome browser [duplicate]

Possible Duplicate:
Safe feature-based way for detecting Google Chrome with Javascript?
Using Javascript to detect Google Chrome to switch CSS

Is there a statement that I can use with javascript, to only execute a code if the user is using a browser other than google chrome?

like image 733
Muhammed Bhikha Avatar asked Dec 20 '12 16:12

Muhammed Bhikha


People also ask

How does a Chrome browser execute JavaScript code?

The browser's built-in interpreter searches for <script> tag or . js file linked with HTML file while loading a web page, and then interpretation and execution starts. Example: In this approach, we have written JavaScript code within the HTML file itself by using the <script> tag.

Why is my JavaScript code not working in Chrome?

Google ChromeIn the "Settings" section click on the "Show advanced settings..." Under the the "Privacy" click on the "Content settings...". When the dialog window opens, look for the "JavaScript" section and select "Allow all sites to run JavaScript (recommended)". Click on the "OK" button to close it.

What is the problem with JavaScript?

These days, most cross-browser JavaScript problems are seen: When poor-quality browser-sniffing code, feature-detection code, and vendor prefix usage block browsers from running code they could otherwise use just fine. When developers make use of new/nascent JavaScript features, modern Web APIs, etc.)


1 Answers

var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;

if(!is_chrome)
{
//do stuff
}  

Edit: Not sure how this will effect chrome mobile browser.

Second Edit: This throws a false positive in Microsoft Edge, the below code is the updated version:

var is_chrome = (typeof window.chrome === "object" && navigator.appVersion.indexOf('Edge') === -1)
like image 152
DickieBoy Avatar answered Sep 27 '22 21:09

DickieBoy