1

I'm trying to get an alert to pop up when the user opens my webpage in a browser other than Chrome. I have this:

if (/what to put here to make the below show only if the browser is not Google Chrome?/) { alert( "Please use Google Chrome to access this site.\nSome key features do not work in browsers other than Chrome." ); }

Found this here as a possible condition: navigator.userAgent.search("Chrome"), but can't make it work. Please advise. :-)

RavMucha
  • 11
  • 2
  • 3
    Possible duplicate of [JavaScript: How to find out if the user browser is Chrome?](https://stackoverflow.com/questions/4565112/javascript-how-to-find-out-if-the-user-browser-is-chrome) – Maxime Peloquin Feb 21 '18 at 14:39
  • 1
    https://stackoverflow.com/questions/4565112/javascript-how-to-find-out-if-the-user-browser-is-chrome might answer your question – Kiran Feb 21 '18 at 14:39
  • 1
    Popups are not the best solution. If possible - better to check if these features are working in the current environment and if not to show let say a div with a warning instead. There may be browsers you don't know where those features are working. – Stoycho Trenchev Feb 21 '18 at 15:13
  • 1
    Better test on features instead of browser type – Codebeat Feb 21 '18 at 15:20

2 Answers2

3

As the comments above point out by referencing questions you can test for chrome in the userAgent. But you would want to reverse that:

    let notChrome = !/Chrome/.test(navigator.userAgent)
    
    let alertMessage = "Please use Google Chrome to access this site.\nSome key features do not work in browsers other than Chrome."
    if(notChrome) alert(alertMessage)

This will solve the problem unless the user is spoofing their userAgent. This is unusual though. To fully check you should also check for a feature chrome has AND the userAgent:

        let notChrome = !/Chrome/.test(navigator.userAgent) && !("webkitAppearance" in document.body.style)
        
        let alertMessage = "Please use Google Chrome to access this site.\nSome key features do not work in browsers other than Chrome."
        if(notChrome) alert(alertMessage)
Jon Wood
  • 1,531
  • 14
  • 24
1

This should to it.

var isChrome = !!window.chrome; // "!!" converts the object to a boolean value
console.log(isChrome); // Just to visualize what's happening

/** Example: User uses Firefox, therefore isChrome is false; alert get's triggered */
if (isChrome !== true) { 
  alert("Please use Google Chrome to access this site.\nSome key features do not work in browsers other than Chrome.");
}
CodeF0x
  • 2,624
  • 6
  • 17
  • 28
  • Nope, doesn't work for some reason. What works is: if(navigator.userAgent.indexOf("Chrome") != -1 ){;} else { alert("Please use Google Chrome...");} //out of FF, IE and Edge only Edge doesn't display the alert, thou that's not a big deal, as all seems to work there fine. – RavMucha Feb 22 '18 at 16:04
  • @RavMucha Hm, that's strange. I tested it in Chrome and FF. FF triggered the alert, Chrome did not. For me it worked. But I'm glad you found another solution. – CodeF0x Feb 22 '18 at 16:14