Wikipedia:Reference desk/Archives/Computing/2021 April 27

From Wikipedia, the free encyclopedia
Computing desk
< April 26 << Mar | April | May >> April 28 >
Welcome to the Wikipedia Computing Reference Desk Archives
The page you are currently viewing is a transcluded archive page. While you can leave answers for any questions shown below, please ask new questions on one of the current reference desk pages.


April 27[edit]

Identifying a user agent using HTML and/or CSS[edit]

Is there a means by which the user agent (i.e. browser) may be identified by means of HTML elements and/or CSS declarations? I've got a rough idea, using this stylesheet:

.useragent { display: none; }
.useragent.useragent-chrome { display: inline; }
.useragent.useragent-firefox { display: inline; }

with this HTML:

<span class="useragent useragent-chrome">Your user agent is Chrome</span>
<span class="useragent useragent-firefox">Your user agent is Firefox</span>

Are there any identifiers which one user agent defines or recognises, but others don't? --Redrose64 🌹 (talk) 14:04, 27 April 2021 (UTC)[reply]

In JavaScript, you can use the following property to ask the browser for its the user agent.
window.navigator.userAgent; // "Mozilla/5.0 (Windows; U; Win98; en-US; rv:0.9.2) Gecko/20010725 Netscape6/6.1"
I don't believe it's possible to detect it using CSS alone, and usually, webpages shouldn't attempt to render differently depending on the user agent. If you want to know whether a browser supports a particular feature, you should do this using CSS feature queries, like the following:
@supports not (display: grid) {
  div {
    float: right;
  }
}
Hope this answer helps, RoxySaunders (talk · contribs) 00:13, 28 April 2021 (UTC)[reply]
Thanks, but I can't write Javascript, and don't intend to try. I did once get a book on Javascript from the library, and tried example 1 - display a button titled "Submit" which if clicked, would display "Hello, world". Clicking it crashed not only my browser but the whole machine, and I lost the word doc that I had open in another window. Not a good sales pitch for javascript if it can cause that much damage. --Redrose64 🌹 (talk) 09:14, 29 April 2021 (UTC)[reply]
This sounds like an incredibly exceptional event, I doubt it'd ever happen again. —moonythedwarf 13:06, 29 April 2021 (UTC)[reply]
It's just as well. Every time a web server tries to recognize details of a particular browser and adjust its behavior in some way, poor Jon Postel spins in his grave. —Steve Summit (talk) 22:17, 29 April 2021 (UTC)[reply]