推荐关注
Meteor
JSLint - a JavaScript code quality tool
jsFiddle
D3.js
WebStorm
推荐书目
JavaScript 权威指南第 5 版
Closure: The Definitive Guide
Pastsong

如何用 Javascript 获取到客户端当前连接的 HTTP 版本?

  •  1
     
  •   Pastsong · Oct 29, 2015 · 2827 views
    This topic created in 3855 days ago, the information mentioned may be changed or developed.

    自己用 Nginx 1.9.6 搭了一个 HTTP/2 的站点,想通过 JS 获取到浏览器当前的 HTTP 协议版本

    var req = new XMLHttpRequest();
    req.open('GET', document.location, false);
    req.send(null);
    var headers = req.getAllResponseHeaders();
    alert(headers);
    

    文档里看到上面的方法可以获取到 header ,但是 header 里并没有 http 的版本号

    2 replies    2015-10-29 16:48:15 +08:00
    Pastsong
        1
    Pastsong  
    OP
       Oct 29, 2015
    想到一种方法,用 nginx 所带的 ngx_http_v2_module 引入的 $http2 变量,设置一个额外的 header ,然后用上述 JS 代码获取到这个 header 的值来确定客户端是否使用 http2 版本。

    http://nginx.org/en/docs/http/ngx_http_v2_module.html
    Pastsong
        2
    Pastsong  
    OP
       Oct 29, 2015


    应该是可行的
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5017 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 39ms · UTC 09:33 · PVG 17:33 · LAX 02:33 · JFK 05:33
    ♥ Do have faith in what you're doing.