3

I have an program that displays content on web browsers via a localhost address. My program is cross-platform and I need to know what OS the browser is running on Windows for my program to operate correctly. I can do that by entering the following command into the browser's dev-tools console:

if (navigator.appVersion.indexOf("Win")!=-1) OSName="Windows";

How can I automate this in my program? I don't know what browser users will be using, or even what OS they'll be on. The reason I can't know what the OS is ahead of time is because of the Windows build. This is done on the Windows Subsystem for Linux which means that the user's browser could be in Ubuntu or (more likely) in Windows. I need to know which one it is to save files correctly.

Alex
  • 2,154
  • 3
  • 26
  • 49
  • 1
    why not read `navigator.appVersion` through javascript on the starting page and then set a cookie with the same? then you server can automatically server it based on the cookie value that you set – Tarun Lalwani Feb 15 '18 at 09:27
  • there's no mention on how your program is being developed, so i presume html for it: `` please provide more details – zhack Feb 20 '18 at 20:36

2 Answers2

3

If I understood correctly - you are basically developing a web application which operates differently on different browsers.

You do not need to use the browser's dev-tools in order to understand which browser is currently being used. In fact, there are several ways to detect user's browser:

  1. Server Side User agent - 'User-Agent' is an HTTP header specifying which browser, version, and OS the user is using. If you control the server application, just extract this header and parse it.
  2. Client Side User Agent - You can use the navigator.userAgent property to retrieve the user agent string via JS.
  3. Browser functionality - Please read this comment.

Of course, there are other, more complicated methods of understanding which browser is being used, but I don't think you will actually need to use them, as the user agent string will probably suffice here.

Walter White
  • 126
  • 1
  • 9
1

I can recommend that you write a small add-on to a browser that will make the identification simple and easy, would you like to have an example?

Sagi Nadav
  • 295
  • 1
  • 3
  • 14