问题描述
是否可以从浏览器内部检测到浏览器对HTTP2/SPDY客户端的支持?
Is it possible to detect a browser's support for HTTP2/SPDY client-side from within the browser?
我试图向用户展示他们的浏览器是否支持HTTP2/SPDY或将使用传统的非HTTP2/SPDY HTTPs协议.
I am attempting to show users whether their browser supports HTTP2/SPDY or would use the traditional, non-HTTP2/SPDY HTTPs protocol.
推荐答案
感谢Patrick.我听取了您的建议,并利用了nginx的$http2
变量,并使用PHP返回了动态JS变量以供浏览器检测. (如果其他读者喜欢,也可以通过传递cookie或添加响应标头进行AJAX检测).
Thanks, Patrick. I took your advice and leveraged nginx's $http2
variable and used PHP to return a dynamic JS variable for the browser's detection. (Passing a cookie or adding a response header for AJAX detection are also options if other readers prefer).
NGINX配置添加
server {
...
if ($http2) {
rewrite ^/detect-http2.js /detect-http2.js.php?http2=$http2 last;
}
# fallback to non-HTTP2 rewrite
rewrite ^/detect-http2.js /detect-http2.js.php last;
# add response header if needed in the future
add_header x-http2 $http2;
}
detect-http2.js.php
<?
header('content-type: application/javascript');
if (isset($_REQUEST['http2'])) {
echo "var h2Version='". $_REQUEST['http2'] . "';\n";
}
?>
detect-http2.html
<html>
<body>
<script src="https://DOMAIN_NAME/detect-http2.js"></script>
<script>
document.write('HTTP2-Supported Browser: '+ (typeof h2Version !== 'undefined'));
</script>
</body>
</html>
这篇关于在浏览器中检测HTTP2/SPDY支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!