Is it possible to determine the HTTP protocol version using JavaScript? I need to detect if the user is using HTTP/2 protocol (and congratulate him if this was the case).
Asked
Active
Viewed 1,762 times
6
-
Is your Javascript code running on the server end? If not (assuming client/browser end), what do you mean by "if the user is using http/2"? – unkhan Feb 27 '15 at 19:47
-
I want to check this on client side. HTTP/2 is a new protocol and few browsers support it. – Salman A Feb 27 '15 at 20:03
-
So what you really want to do is check what browser the user has (http://stackoverflow.com/questions/5916900/how-can-you-detect-the-version-of-a-browser) and then check if its one of the versions listed here http://caniuse.com/#feat=spdy – unkhan Feb 27 '15 at 20:10
2 Answers
5
console.log(performance.getEntries()[0].nextHopProtocol)
See also: https://caniuse.com/mdn-api_performanceresourcetiming_nexthopprotocol
Works in
- Edge since 17
- Firefox (Desktop and Mobile) since 45
- Chrome (Desktop and Mobile) since 61
- Safari Mac 14.1 (tested here, but not yet updated on MDN/caniuse)

bb.
- 1,371
- 1
- 14
- 21

everconfusedGuy
- 2,709
- 27
- 43
-4
if (location.protocol == "http/2"){
alert('congratulations!');
}

Anthony R Gray
- 93
- 4
-
For http and https protocols, this shows `http:` and `https:`, respectively - should it not be `http/2:`, then? Also, a link to some official documentation would be much valuable for future readers. – Robert Rossmann Feb 27 '15 at 20:35
-
you are correct, Robert Rossman. I left out the colon in the protocol. I am not sure about the new protocol because I haven't seen it so I went with how the OP presented it. – Anthony R Gray Feb 27 '15 at 20:41