There are good compression components on the Internet, but I personally feel it is quite troublesome to configure a lot of things and will compress the files in the entire folder. The Php website provides a parameter passing method, I have mentioned this in my previous files, but I have not provided a complete solution. Today, I am posting the Code directly for your reference and learning. I am directly going to the Code:
The Urlwriter component is used to redirect the file to be compressed, as shown in the following example:
Script type = "text/javascript" src = "http: // localhost/NhibernateWeb/Js/jquery? V = js/jquery-1.7.2.js, js/validate/jquery. metadata. js "> </script>
1. web. config
[Html]
<ConfigSections>
<! -- Set the address rewriting component -->
<Section name = "rewriter" requirePermission = "false" type = "Intelligencia. UrlRewriter. Configuration. RewriterConfigurationSectionHandler, Intelligencia. UrlRewriter"> </section>
</ConfigSections>
<! -- Rewriter config -->
<Rewriter file = "~ /App_Data/rewrite. xml "/>
2. rewrite the rewrite. xml file.
[Html]
<? Xml version = "1.0" encoding = "UTF-8"?>
<RewriteRules>
<Rewrite url = "^ ~ /Js/jquery \? V = ([0-9a-zA-Z \.,-\/] +) $ "to = "~ /MegerCompress. ashx "/>
</RewriteRules>
3. Front-End file MegerJsCss. aspx
[Html]
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> </title>
<Link type = "text/css" href = "http: // localhost/NhibernateWeb/PagecontrolStyle.css"/>
<Link type = "text/css" href = "Css/index.css" rel = "Stylesheet"/>
<Script type = "text/javascript" src = "http: // localhost/NhibernateWeb/Js/jquery? V = js/jquery-1.7.2.js, js/validate/jquery. metadata. js "> </script>
<Script type = "text/javascript">
$ (Function (){
JQuery. mytest = function (){
Alert ("OK ");
}
})
$ (Function (){
$. Mytest ();
})
</Script>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Div style = "width: 100%; height: 500px;">
</Div>
</Form>
</Body>
</Html>
4. MegerCompress. ashx
[Vb]
Imports System. Web
Imports System. Web. Services
Imports System. Collections. Generic
Imports System. Text
Imports System. IO
Imports Yahoo. Yui. Compressor
Public Class MegerCompress
Implements System. Web. IHttpHandler
Sub ProcessRequest (ByVal context As HttpContext) Implements IHttpHandler. ProcessRequest
Context. Response. ContentType = "text/javascript"
'Context. Response. Write ("function f (){}")
Dim s As String = context. Request ("v ")
Dim jspath () As String = s. Split (",")
Dim strContent As New StringBuilder
For I As Integer = 0 To jspath. Length-1
Next
Dim fpath As String = System. Web. HttpContext. Current. Server. MapPath (jspath (0 ))
If File. Exists (fpath) Then
'Read text
Dim sr As New StreamReader (fpath, System. Text. Encoding. UTF8)
Dim str As String = sr. ReadToEnd ()
Sr. Close ()
StrContent. Append (str)
End If
Dim js As New JavaScriptCompressor (strContent. ToString (), False, Encoding. UTF8, System. Globalization. CultureInfo. CurrentCulture)
Context. Response. Write (js. Compress)
End Sub
ReadOnly Property IsReusable () As Boolean Implements IHttpHandler. IsReusable
Get
Return False
End Get
End Property
End Class
Share personal test results:
Code behavior page size time
1 Load 490.7 K 500 ms (onload2.45s) one by file path)
2 dynamic loading (above) 334.1 1.97 s (onload3.73s)
3. Compress a file with a tool for 334.1 ms (onload3.07s)
Conclusion The above method can achieve dynamic loading on demand, but the problem is that the efficiency is not very high. It is best to use tools to compress the required files and load the compressed files.