0

I am trying to implement a function to detect chrome browser and then execute a particular function on the fly. We have a webpage where document links are displayed and on click on it, the document is downloaded.

the requirement is to enable the kiosk machines which uses chrome browser to open a document in a new tab as the download and opening the document is restricted in Kiosk machines.

to determine whether the browser is chrome, i have used below approaches.

  1. using userAgent -- below approach returns true for other browsers as well

    this.isChromeBrowser = (/(?!.*chrome).*/i).test(navigator.userAgent);

  2. using window.chrome, below code was working when i implemented, but now it is giving issues and it returns false for chrome browser. (window.chrome.webstore & runtime are undefined).

this.isChromeBrowser = !!window.chrome && (!!window.chrome.webstore || !!window.chrome.runtime);

  1. i am thinking of using implementation 1 along with window.navigator.vendor

this.isChromeBrowser = /Chrome/.test(navigator.userAgent) && /Google Inc/.test(navigator.vendor);

or

this.isChromeBrowser = /Chrome/.test(navigator.userAgent) && /Google Inc/.test(navigator.vendor) && window.chrome;

Need some inputs on which would be a better solution to use. i am more inclined towards implementation 3, as we would be checking the useragent as well as the vendor.

============ update : the implementation and extensive explanation is already provided in the below link. this give my answer.

JavaScript: How to find out if the user browser is Chrome?

bharath c
  • 89
  • 8
  • 1
    did you read all in here https://stackoverflow.com/questions/4565112/javascript-how-to-find-out-if-the-user-browser-is-chrome/13348618#13348618 – canbax Dec 09 '19 at 12:58
  • @canbax thanks foe the pointers, the link answers all my questions. – bharath c Dec 09 '19 at 13:13
  • Does this answer your question? [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) – canbax Dec 09 '19 at 14:09

2 Answers2

0

I have used this in the past:

if(navigator.userAgent.search("Chrome") > 0){
   //yourfunction
}
tlt
  • 358
  • 4
  • 10
0

update the links which already answers the question which i posted,

bharath c
  • 89
  • 8