abagshaw
abagshaw

Reputation: 6602

Javascript test client side for HTTP Client Hints support

Is it possible to detect if a browser supports HTTP Client Hints using javascript? Right now only chrome appears to support it: http://caniuse.com/#feat=client-hints-dpr-width-viewport

So I was thinking of using some javascript library that can do browser and version detection and if the browser is chrome and version 49 or later than I could assume the feature was supported.

I'm just thinking this solution isn't very efficient or smart considering functionality for HTTP Client Hints will most likely be added to more browsers in the future and then I would have to continually update my function to reflect that.

Is there some simple way to just test if a given browser supports HTTP Client Hints with client side javascript?

Thanks!

Upvotes: 2

Views: 515

Answers (2)

Luca P.
Luca P.

Reputation: 1071

This question is old (2017) and a lot has changed wrt Client-Hints. The most important thing is that one can no longer sit passively and expect to receive client-hints in the HTTP header. Now there's a rather complex HTTPS dance in which web servers need to be configured to actively request Client-Hints and cross their fingers that clients honor their demand. I know because my company operates in the field of device/browser data based on HTTP request analysis (WURFL) .

Specific information about how to request Client-Hints from web browsers is available here: https://www.scientiamobile.com/add-support-for-user-agent-client-hints-now/

Upvotes: 0

Apokalyptik
Apokalyptik

Reputation: 31

Sadly it appears the only way to detect if a browser supports this feature is by checking the make and model of the browser... Something like this...

function canDoClientHint() {
    try {
        var chrome = window.navigator.userAgent.match(/\sChrome\/([0-9]+)\.[.0-9]+\s/)
        if ( chrome !== null ) {
            var version = parseInt(chrome[1])
            if ( isNaN(version) === false && version >= 46 ) {
                return true
            }
        }
    } catch(e) {
        return false
    }
    return false
}
canDoClientHint()

Obviously this kind of code becomes outdated and requires constant maintenance until all the browsers you care have the feature generally available and then you can remove it. Unfortunately you have to remember to update it and remove it.

Upvotes: 0

Related Questions