0

I would like to implement a way in javascript to check if a user is viewing the document in IE, Mozilla or Chrome. The reason is because the key event trigger behaves differently in different browser. Thus, if I can detect what sort of browser, I can work around this issue. I just want to know what type of browser, not what browser version is being used.

From what I have researched, it seems I have to use some sort of library call navigator.userAgent. I'm unfamiliar with this since this is new to me.

Link: How to detect Safari, Chrome, IE, Firefox and Opera browser?

The idea of what I want to implement goes like this:

if(user detected using Firefox)
function1()

if(user detected using Chrome)
function2()

if(user detected using IE)
function3()
.....

What is the proper way to use this library or is there any other way to detect the browser type?

Daredevil
  • 1,672
  • 3
  • 18
  • 47
  • Look here: https://developer.mozilla.org/en-US/docs/Web/HTTP/Browser_detection_using_the_user_agent – Bibberty Feb 20 '19 at 05:24
  • @Bibberty But in the link, it's meant for expression while my problem lies on the keycode that each browser has different name for it – Daredevil Feb 20 '19 at 05:28
  • 1
    Do not do "what browser is being used". Browsers lie. Implement key event processing using standards based properties, and fall back to legacy and proprietary properties if you must. – traktor Feb 20 '19 at 05:29
  • @traktor53 What do you mean? – Daredevil Feb 20 '19 at 05:33
  • 1
    He means rely on the functions available and not what the browser agent says it is. There are many robots etc. But to be fair, do you care about those that imitate?. For example I scape an IP Geolocation site and fake the chrome browser. – Bibberty Feb 20 '19 at 05:36
  • @Daredevil I mean do not start\ by testing deprecated properties. Start by testing to see if [event.key](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key) exists and use if if it does. Only check deprecated event properties like `which`, `code` and `keycode` if `key` is falsey. The number of [browser sou need to do this in](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key#Browser_compatibility) may be less than you think. – traktor Feb 20 '19 at 06:10
  • I see. Based on your link you sent, I only needed to use Printscreen(44) and Windows key(91/92) but it seems IE doesn't detect this. – Daredevil Feb 20 '19 at 06:35

0 Answers0