简体   繁体   English

如何将gzip Javascript放入网站。

[英]How do I put a gzip Javascript into website.

I am newby to speeding up web pages and I was wondering if anyone could tell me how to put a compressed gzip javascript file into my html? 我是来加速网页的新手,我想知道是否有人可以告诉我如何将压缩的gzip javascript文件放入我的html中?

I have compressed the js using YUI Compressor which gave me a gzip file and my current js is located at: 我已经使用YUI Compressor压缩了js,这给了我一个gzip文件,而我当前的js位于:

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

Do I just redirect the src to the new gzip file? 是否只将src重定向到新的gzip文件? And if I do that what happens if a browser can't read the file? 如果我这样做,如果浏览器无法读取文件,该怎么办? Any help would be greatly appreciated and thanks in advance! 任何帮助将不胜感激,并在此先感谢!

I have also gzipped the rest of the site by putting the following into the htaccess: 我还通过将以下内容放入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 

Gzip compression happens on the fly, you don't need to change the file paths or src. Gzip压缩是即时进行的,您无需更改文件路径或src。 It happens automatically between your server and the browser. 它会在服务器和浏览器之间自动发生。 There is no need to worry about browsers that don't support gzip, it's very likely they don't exist anymore , such as Netscape 4.0, Opera 4.0 and IE 4.0. 无需担心不支持gzip的浏览器,很有可能它们不再存在 ,例如Netscape 4.0,Opera 4.0和IE 4.0。

Your code .htaccess code above should work fine. 您上面的代码.htaccess代码应该可以正常工作。


Update: 更新:

I ran a quick test and currently your HTML is successfully being transmitted with gzip, seems like only JS files are not yet compressed. 我进行了快速测试,目前您的HTML已成功通过gzip传输,似乎只有JS文件尚未压缩。 This line in your .htaccess is the culprit <files *.html> , it is only selecting HTML files for gzip compression. .htaccess中的这一行是罪魁祸首<files *.html> ,它只是选择HTML文件进行gzip压缩。

I updated your config, try this one: 我更新了您的配置,请尝试以下操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Facebook iframe 没有加载到我的网站上。 我如何解决它? - Facebook iframe not loading on my website. How do I fix it? 我如何在我的网站上有多个手风琴下拉菜单。 HTML,CSS JavaScript - How can I have multiple accordion drop down menus in my website. HTML, CSS JavaScript PageSpeed Insights在我的网站中检测到Render Blocking JS和CSS。 我该如何解决? - PageSpeed Insights Detects Render Blocking JS and CSS in my Website. How do I fix this? 我在网站上添加了在线JavaScript。 如何更改该JavaScript添加的网页中的HTML? - I added online JavaScript in my website. How can I change HTML in my webpage which is added by that JavaScript? 在此网站上运行的交互式动画角色。 这个怎么做? - Interactive animated characters running on this website. How to do this? 刷新网站时随机生成的数字不会改变。 我正在使用 JavaScript 和 NodeJS - Randomly generated numbers won't change when I refresh website. I'm using JavaScript and NodeJS 我怎么把br放在javascript .text中 - how do i put br in javascript .text 如何将会话值放入JavaScript? - How do I put a session value into a javascript? 如何在JavaScript的字符串中添加反斜杠? - How do I put backslash in strings in javascript? 如何在我的网站上放置具有不同声音的各种按钮? - How do I put various buttons with different sounds on my website?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM