JavaScript可以检测用户的浏览器是否支持gzip吗? [英] Can JavaScript detect if the user's browser supports gzip?

查看:195
本文介绍了JavaScript可以检测用户的浏览器是否支持gzip吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用JavaScript来检测用户浏览器是否支持压缩的内容(客户端,而不是node.js或类似的内容)?

Can I use JavaScript to detect if the user's browser supports gzipped content (client side, not node.js or similar)?

我正在尝试支持以下极端情况:
有许多可能的文件可以加载到特定的Web应用程序上,最好在应用程序运行时根据需要按需加载它们,而不是一开始就加载它们.我想使用远期的缓存过期日期为S3提供这些文件.由于 S3不支持gzip将文件压缩到支持它的客户端,因此我想托管两个版本的每个文件-一个普通文件,以及一个将 content-type 设置为 application/gzip 的文件压缩.浏览器当然需要知道要请求哪些文件.如果JavaScript能够检测到浏览器是否支持压缩的内容,则浏览器将能够请求正确的文件.

I am trying to support the following edge case:
There are a lot of possible files that can load on a particular web app and it would be better to load them on demand as necessary as the application runs rather than load them all initially. I want to serve these files off of S3 with a far-future cache expiration date. Since S3 does not support gzipping files to clients that support it, I want to host two versions of each file -- one normal, and one gzipped with content-type set to application/gzip. The browser of course needs to know which files to request. If JavaScript is able to detect if the browser supports gzipped content, then the browser will be able to request the correct files.

这可能吗?

推荐答案

JavaScript无法,但是您可以使用Javascript检测浏览器是否支持压缩的内容.

Javascript can't, but you can use Javascript to detect wether or not the browser supports gzipped content.

我在上面评论过,并且只想重申一下,无论如何,您都应该使用CloudFront,它可以处理gzip内容.如果您使用的是S3,那么出于回答您的问题的目的,您有零个理由不愿使用CloudFront ...

I commented above and would just like to reiterrate, you should use CloudFront anyway, which does gzip content. If you are using S3, then there is zero reason why you would not want to use CloudFront, however, for the purposes of answering your question...

此博客文章完美地介绍了如何检测浏览器是否支持Gzip.

This blog post perfectly addresses how you would detect if the browser supports Gzip.

http://blog.kenweiner.com/2009/08/serving-gzipped-javascript-files-from.html

这里是一个简短的摘要:

Here is a quick summary:

1)创建一个小的gzip压缩文件gzipcheck.js.jgz,并将其在CloudFront中可用.该文件应包含一行代码:

1) Create a small gzipped file, gzipcheck.js.jgz, and make it available in CloudFront. This file should contain one line of code:

gzipEnabled = true;

2)使用以下代码尝试加载和运行此文件.您可能希望将其放在其他任何Javascript代码之前的HTML HEAD部分中.

2) Use the following code to attempt to load and run this file. You'll probably want to put it in the HTML HEAD section before any other Javascript code.

<script type="text/javascript" src="gzipcheck.js.jgz">
</script>

如果文件加载,它将设置一个标志gzipEnabled,该标志指示浏览器是否支持gzip.

If the file loads, it sets a flag, gzipEnabled, that indicates whether or not the browser supports gzip.

这篇关于JavaScript可以检测用户的浏览器是否支持gzip吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆