Salman Arshad
Salman Arshad

Reputation: 272066

How to get the HTTP protocol version via JavaScript

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).

Upvotes: 7

Views: 2455

Answers (2)

everconfusedGuy
everconfusedGuy

Reputation: 2797

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)

Upvotes: 7

Anthony R Gray
Anthony R Gray

Reputation: 93

if (location.protocol == "http/2"){
    alert('congratulations!');
}

Upvotes: -4

Related Questions