Check if the browser is not Google Chrome before running the code in JavaScript

Related Search:
Finding a secure way to detect Google Chrome using Javascript?
Using Javascript to identify Google Chrome and apply CSS changes

Is there a JavaScript statement that can be used to execute code only when the user is not using Google Chrome as the browser?

Answer №1

let checkForChrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;

if (!checkForChrome) {
//perform certain actions
}

Note: It is uncertain how this will impact the performance on the Chrome mobile browser.

Update: This code snippet might not work as expected in Microsoft Edge. Here is the revised version:

let checkForChrome = (typeof window.chrome === "object" && navigator.appVersion.indexOf('Edge') === -1)

Answer №2

Yes, I occasionally utilize this JavaScript code to verify browser compatibility.

var isIE6 = false /*@cc_on || @_jscript_version < 5.7 @*/
var isIE7 = (document.all && !window.opera && window.XMLHttpRequest && navigator.userAgent.toString().toLowerCase().indexOf('Trident/4.0') == -1) ? true : false;
var isIE8 = (navigator.userAgent.toString().toLowerCase().indexOf('trident/4.0') != -1);
var isIE9 = (navigator.userAgent.toString().toLowerCase().indexOf('trident/5.0') != -1);
var isSafari = (navigator.userAgent.toString().toLowerCase().indexOf("safari") != -1) && (navigator.userAgent.toString().toLowerCase().indexOf("chrome") == -1);
var isFirefox = (navigator.userAgent.toString().toLowerCase().indexOf("firefox") != -1);
var isChrome = (navigator.userAgent.toString().toLowerCase().indexOf("chrome") != -1);
var isAndroid= (navigator.appVersion.toString().indexOf("Android", 0)!=-1)
var isMobileSafari = ((navigator.userAgent.toString().toLowerCase().indexOf("iphone")!=-1) || (navigator.userAgent.toString().toLowerCase().indexOf("ipod")!=-1) || (navigator.userAgent.toString().toLowerCase().indexOf("ipad")!=-1)) ? true : false;

In your JavaScript code, utilize these variables with appropriate if statements.

Answer №3

Verifying the existence of the window.chrome property serves as a reliable method for identifying Chrome without depending on the user agent string:

if(typeof window.chrome != "object") {
    // browser is not Chrome
} else {
    // browser is Chrome (or a derivative of Chromium)
}

Answer №4

While some responses to this inquiry hold merit, I strongly advise against relying on "browser sniffing" unless absolutely necessary. For more information, visit: http://www.sitepoint.com/why-browser-sniffing-stinks/

Instead, consider utilizing useful resources like Modernizr to determine if a user's browser can support specific features.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Troubleshooting the issue of browser prefix injections not functioning properly in Vue when using the

I've spent an entire afternoon on this issue and I'm completely stuck. After realizing that IE11 doesn't support grid-template, I learned that I need to use -ms-grid-columns and -ms-grid-rows instead. I am attempting to generate CSS and inje ...

Revamping the functionality of autocomplete search

Presently, my code includes: $("#location").autocomplete({source: cities, minLength: 0, autoFocus: true}); The issue with this plugin is that it searches for matches anywhere within the input text. For instance, when I type "Bos", it not only suggests w ...

Combine activities from a dynamic array of Observables

I'm currently utilizing the rxjs library. In my application, I have a Browser object overseeing multiple instances of Page objects. Each page emits a stream of events through an Observable<Event>. Pages can be opened and closed dynamically, le ...

Modifying TextField color in a react application with Material UI

I have a react component that consists of a text field and a button. I want these elements to be displayed in green color on a black background, but I am unable to modify the default colors of all the elements. Following a similar query on how to change th ...

Why isn't my HTML list showing up on Firefox mobile browsers?

I am using jQuery mobile version 1.4.2. Here is the code snippet from my page. HTML <ul data-role="listview" class="ui-nodisc-icon ui-alt-icon"> <li><a href="#" >Real Estate in San Jose</a></li> </ul> CSS .ui-list ...

What is the best way to horizontally center an absolute button within a div?

I'm currently attempting to center an absolute button within a div at the bottom, but unfortunately it doesn't seem to be working as expected. Here is my current approach: .mc-item { background: #F0F0F0; border: 1px solid #DDD; height: 1 ...

Ensuring that the bootstrap5 column extends all the way to the footer

Looking to display a specific email address mailbox for a project. The goal is to have the 'Mail Card List' scrollable instead of extending beyond the page. Tried using 'max-height:;' CSS, but it sets a fixed height. Seeking a variable ...

Need help tackling this issue: getting the error message "Route.post() is asking for a callback function, but received [object Undefined]

I am currently working on implementing a new contactUs page in my project that includes a form to store data in a mongoDB collection. However, after setting up all the controller and route files and launching the app, I encountered an error that I'm u ...

Using Angular to bind the ngModel to a variable's object property

In my application, I am working with a user object that looks like this: let user = {name: "John", dob:"1995-10-15", metadata: {}} The metadata property of the user object is initially empty. I want to add a new property to the metadata object based on u ...

Ways to customize the size of a radio button with CSS

Is it possible to adjust the size of a radio button using CSS? ...

Increase the gap between the legend and the chart when utilizing charts.js

I'm currently working on a project using charts.js and running into a slight issue. The legend for my bar chart is overlapping with the values displayed. I've been attempting to troubleshoot this problem without much success so far, so I would g ...

Tips for postponing an action until the webpage is fully loaded

Currently, I am using Selenium in R to perform a specific task. The script I have written enables the program to search for pizza restaurants near a designated geographical coordinate on Google Maps. The script continues to scroll until all restaurant inf ...

What is the best way to send the selected option from a dropdown to a button click function within the controller?

I need to pass the selected value from a user's country selection dropdown to the ng-click="saveChanges()" function on the submit button. Is there a correct method for achieving this? I want to be able to access the user's selection from the dro ...

JQuery malfunctions within an Asp.net control after being loaded through a partial postback

Working on an application using ASP.net/C#. Utilizing an update panel and a Placeholder within it to dynamically load controls <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional"> <Triggers> <asp:AsyncPost ...

Select a different radio button to overwrite the currently checked one

I am facing a situation where I have two radio buttons. The default checked state is one of them, but I want to change that and make the other one checked by default instead. Here is the HTML code: <div class="gui-radio"> <input type="radio" v ...

Binding attributes in knockoutjs following an AJAX request

The objective Incorporate the attr binding of KnockoutJS following an AJAX call. The dilemma Check out the code snippet below: $.ajax({ url: "/Products/List?Output=JSON", dataType: "json", success: function (data) { $.each(data, fun ...

JQuery cannot target content that is dynamically inserted through an Ajax request

When I use an ajax call to pass dynamically generated html, such as in the example below: var loadContent = function(){ $.ajax({ url: '/url', method: 'GET' }).success(function (html) { $('.con ...

What is the process of retrieving a property value from a database using selected values from cascaded drop-down lists?

I am facing a challenge where I need to extract a property of an entity by passing the IDs of selected items from a cascaded dropdown list. The requirement is to update the price every time there is a change in either level 1 or level 2 of the cascading dr ...

Encountering a missing value within an array

Within my default JSON file, I have the following structure: { "_name":"__tableframe__top", "_use-attribute-sets":"common.border__top", "__prefix":"xsl" } My goal is to add values by creating an array, but I am encountering an issue where my ...

Listening for a client's socket emit through Express Routes

I have successfully implemented the functionality to emit and receive messages using socket.io between the server and client with the code in server.js. const express = require('express') const app = express() const port = 4000 var http = require ...