Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/429.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将gzip Javascript放入网站。_Javascript_Html_Gzip_Gzipfile - Fatal编程技术网

如何将gzip Javascript放入网站。

如何将gzip Javascript放入网站。,javascript,html,gzip,gzipfile,Javascript,Html,Gzip,Gzipfile,我对加快网页速度很陌生,我想知道是否有人能告诉我如何将压缩的gzip javascript文件放入我的html中 我已经使用YUI Compressor压缩了js,YUI Compressor提供了一个gzip文件,我当前的js位于: <script type="text/javascript" src="js/jquery.flexslider.js"> 我是否将src重定向到新的gzip文件?如果我这样做了,如果浏览器无法读取文件会发生什么?任何帮助都将不胜感激,并提前表示

我对加快网页速度很陌生,我想知道是否有人能告诉我如何将压缩的gzip javascript文件放入我的html中

我已经使用YUI Compressor压缩了js,YUI Compressor提供了一个gzip文件,我当前的js位于:

<script type="text/javascript" src="js/jquery.flexslider.js">

我是否将src重定向到新的gzip文件?如果我这样做了,如果浏览器无法读取文件会发生什么?任何帮助都将不胜感激,并提前表示感谢

我还通过将以下内容放入htaccess,压缩了站点的其余部分:

<ifModule mod_gzip.c>
mod_gzip_on Yes
mod_gzip_dechunk Yes
mod_gzip_item_include file .(html?|txt|css|js|php|pl)$
mod_gzip_item_include handler ^cgi-script$
mod_gzip_item_include mime ^text/.*
mod_gzip_item_include mime ^application/x-javascript.*
mod_gzip_item_exclude mime ^image/.*
mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</ifModule>

(hashtag) compress text, html, javascript, css, xml:
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript

(hashtag) Or, compress certain file types by extension:
<files *.html>
SetOutputFilter DEFLATE
</files>

(hashtag) remove browser bugs
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
Header append Vary User-Agent<ifModule mod_gzip.c>
mod_gzip_on Yes
mod_gzip_dechunk Yes
mod_gzip_item_include file .(html?|txt|css|js|php|pl)$
mod_gzip_item_include handler ^cgi-script$
mod_gzip_item_include mime ^text/.*
mod_gzip_item_include mime ^application/x-javascript.*
mod_gzip_item_exclude mime ^image/.*
mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</ifModule>

(hashtag) compress text, html, javascript, css, xml:
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript

(hashtag) Or, compress certain file types by extension:
<files *.html>
SetOutputFilter DEFLATE
</files>

(hashtag) remove browser bugs
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
Header append Vary User-Agent 

mod_gzip_on Yes
是的
mod|gzip|item|u include file.(html?| txt | css | js | php | pl)$
mod_gzip_item_包含处理程序^cgi脚本$
mod_gzip_项目包括mime^text/*
mod_gzip_item_包括mime^application/x-javascript*
mod_gzip_项_排除mime^image/*
mod_gzip_item_排除rspheader^内容编码:.*gzip*
(hashtag)压缩文本、html、javascript、css、xml:
AddOutputFilterByType泄气文本/普通
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE应用程序/xml
AddOutputFilterByType DEFLATE应用程序/xhtml+xml
AddOutputFilterByType DEFLATE应用程序/rss+xml
AddOutputFilterByType DEFLATE应用程序/javascript
AddOutputFilterByType DEFLATE应用程序/x-javascript
(hashtag)或按扩展名压缩某些文件类型:
SetOutputFilter放气
(标签)删除浏览器错误
浏览器匹配^Mozilla/4 gzip纯文本/html
浏览器匹配^Mozilla/4\.0[678]无gzip
BrowserMatch\bMSIE!没有gzip!仅限gzip文本/html
头附加用户代理
mod_gzip_on Yes
是的
mod|gzip|item|u include file.(html?| txt | css | js | php | pl)$
mod_gzip_item_包含处理程序^cgi脚本$
mod_gzip_项目包括mime^text/*
mod_gzip_item_包括mime^application/x-javascript*
mod_gzip_项_排除mime^image/*
mod_gzip_item_排除rspheader^内容编码:.*gzip*
(hashtag)压缩文本、html、javascript、css、xml:
AddOutputFilterByType泄气文本/普通
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE应用程序/xml
AddOutputFilterByType DEFLATE应用程序/xhtml+xml
AddOutputFilterByType DEFLATE应用程序/rss+xml
AddOutputFilterByType DEFLATE应用程序/javascript
AddOutputFilterByType DEFLATE应用程序/x-javascript
(hashtag)或按扩展名压缩某些文件类型:
SetOutputFilter放气
(标签)删除浏览器错误
浏览器匹配^Mozilla/4 gzip纯文本/html
浏览器匹配^Mozilla/4\.0[678]无gzip
BrowserMatch\bMSIE!没有gzip!仅限gzip文本/html
头附加用户代理

Gzip压缩是动态进行的,您不需要更改文件路径或src。它会在服务器和浏览器之间自动发生。不需要担心不支持gzip的浏览器,很可能是它们,比如Netscape 4.0、Opera 4.0和IE 4.0

上面的代码.htaccess应该可以正常工作


更新:

我运行了一个快速测试,目前您的HTML正在通过gzip成功传输,似乎只有JS文件尚未压缩。您的
.htaccess
中的这一行是罪魁祸首
,它只是选择HTML文件进行gzip压缩

我更新了您的配置,请尝试以下配置:

<IfModule mod_deflate.c>
    #The following line is enough for .js and .css
    AddOutputFilter DEFLATE js css
    AddOutputFilterByType DEFLATE text/plain text/xml application/xhtml+xml text/css text/javascript application/xml application/rss+xml application/atom_xml application/javascript application/x-javascript application/x-httpd-php application/x-httpd-fastphp text/html

    #The following lines are to avoid bugs with some browsers
    BrowserMatch ^Mozilla/4 gzip-only-text/html
    BrowserMatch ^Mozilla/4\.0[678] no-gzip
    BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
</IfModule>

#下面的行对于.js和.css已经足够了
AddOutputFilter DEFLATE js css
AddOutputFilterByType DEFLATE text/plain text/xml application/xhtml+xml text/css text/javascript application/xml application/rss+xml application/atom_xml application/javascript application/x-javascript application/x-httpd-php application/x-httpd-fastphp text/html
#以下几行是为了避免某些浏览器出现bug
浏览器匹配^Mozilla/4 gzip纯文本/html
浏览器匹配^Mozilla/4\.0[678]无gzip
BrowserMatch\bMSIE!没有gzip!仅限gzip文本/html

谢谢,詹姆斯。我最初也这么认为,但当我运行google页面速度时,我得到了以下建议:对该页面的建议允许压缩以下资源,以将其传输大小减少14.6KiB(减少70%)。压缩可以节省14.6KiB(减少70%)。您好@Michael,我知道怎么了,您的
.htaccess
仅为
.html
文件指定了gzip压缩。试试我发布的版本,让我知道它是否有效。谢谢James,我尝试了你发布的版本,它返回了一个内部服务器错误Hi@Michael,看起来你上面发布的版本有错误。我只是用一个有效的gzip代码修改了我的答案。我尝试了修改,但仍然没有压缩Javascript