1

How can I make this way of detecting Chrome to do something else if the user is using any other browser?, like if is chrome show message, but if is any other browser show another message.

var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
if (is_chrome) {
    document.write("Chrome message")
}
  • 3
    Have you tried using `else`? – jonrsharpe Apr 07 '19 at 11:44
  • 1
    by the way, that's a poor way of detecting chrome anyway ... Edge has Chrome in the useragent (and Safari too ... Microsoft are absolute numpties, putting stuff in their useragent that simply doesn't belong there) – Jaromanda X Apr 07 '19 at 11:48

1 Answers1

1

Use else block, e.g.

if (is_chrome) {
    document.write("Chrome message");
} else {
    document.write("Another browser");
}

If you want to get other specific browsers you can use JS to detect them the same way with elseif:

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

// Opera 8.0+
var isOpera = (!!window.opr && !!opr.addons) || !!window.opera || navigator.userAgent.indexOf(' OPR/') >= 0;

// Firefox 1.0+
var isFirefox = typeof InstallTrigger !== 'undefined';

if (is_chrome) {
    document.write("Chrome message");
} else if (isOpera) {
    document.write("Opera browser");
} else if (isFirefox) {
    document.write("Firefox browser");
} else {
    document.write("Another browser");
}