Best practice to check for Mac vs Windows with javascript (HtmlDialog)

It seems there are potentially several ways to check the current platform’s OS using javascript as shown here:

Methods include navigator.appVersion, navigator.platform, and navigator.userAgent

They seem to all involve getting a string from the methods above and checking if the string contains the desired regular expression such as ‘Mac’ or ‘Win’.

My question is, with respect to HtmlDialogs and SketchUp version compatibility, is there a recommended way to check whether the user is on Mac vs Windows? Or are all of the methods above just as effective / compatible as the other?

I know how to check using Ruby but in my case, my dialog is hosted on a remote server and I only want to affect the server-side code, not the client-side extension code.

You could dynamically create your dialog url from ruby to add a query parameter indicating if it is mac or windows.

Something along the lines of: https://example.com/?os=mac

Doing so your server side code can see this as a query parameter. Now, is it really server side (as in php, .net, java,…) you need this, or web client side (javascript)?

If pure client side, can’t you just set it as a parameter (with execute_script) whenever you send your app’s state to it? I assume you will also have some other state to send to it when the dialog finished loading.

1 Like

Good question. I think I’ve needed to detect platform myself in some of my extensions, but I cannot remember which ones to check.

I looked up navigator.platform and it’s marked as deprecated, MDN recommend not to rely on it: Navigator.platform - Web APIs | MDN

Not sure if that’s likely to change in reality though (because Hyrum’s Law). But, parsing that looks a lot easier than userAgent. AFAIK, from a browser, parsing one of these strings is the only way to get a hint of what platform you’re on.

If you are using a JS library already I’d check if they already have something like that. Relying on whatever someone else has worked out. Otherwise perhaps find a min-library to do it. I would prefer that rather than making assumptions myself, these strings has a tendency to be every so fluid.

Thanks for the input Thom