I’ve found that by optimizing my javascript and CSS files a bit, I can decrease the load times of my pages by quite a bit.
Combining script files, and removing a few elements decreased the total filesize by 36%.
Here is how I do it!
Using it!
-
<!–#include virtual="/includes/server/classes/compress.asp"–>
-
<%
-
Set objCompress = New clsCompress
-
Dim FilePath : FilePath = Split(Request.QueryString("file"), ",")
-
objCompress.strFile = FilePath
-
objCompress.ReturnScripts()
-
Set objCompress = Nothing
-
%>
{please note, you will have to change the location of the include file.}
Include File
-
<%
-
Class clsCompress
-
Public strFile
-
Private objFSO, objFile, objRegExp, strCompressedFile, strFileType
-
Private Sub Class_Initialize()
-
strCompressedFile = ""
-
End Sub
-
Public Sub ReturnScripts()
-
Set objFSO = CreateObject("Scripting.FileSystemObject")
-
If IsArray(strFile) Then
-
For i = 0 To UBound(strFile)
-
Set objFile = objFSO.OpenTextFile(Server.MapPath(strFile(i)))
-
If Not(objFile.AtEndOfStream) Then
-
strCompressedFile = strCompressedFile & VbCrLf & objFile.ReadAll
-
End If
-
Set objFile = Nothing
-
tmp = Right(strFile(i), 3)
-
strFileType = Replace(tmp, ".", "")
-
Next : i = Null
-
Erase strFile
-
End If
-
Set objFSO = Nothing
-
Select Case UCase(strFileType)
-
Case "CSS" ‘CSS
-
Set objRegExp = New RegExp
-
objRegExp.IgnoreCase = True
-
objRegExp.Global = True
-
objRegExp.Pattern = "/\*[^*]*\*+([^/][^*]*\*+)*/"
-
strCompressedFile = objRegExp.Replace(strCompressedFile, "")
-
Set objRegExp = Nothing
-
‘Strip Line Breaks
-
Set objRegExp = New RegExp
-
objRegExp.IgnoreCase = True
-
objRegExp.Global = True
-
objRegExp.Pattern = "\n+"
-
strCompressedFile = objRegExp.Replace(strCompressedFile, "")
-
Set objRegExp = Nothing
-
‘Strip Tabs
-
Set objRegExp = New RegExp
-
objRegExp.IgnoreCase = True
-
objRegExp.Global = True
-
objRegExp.Pattern = "\t+"
-
strCompressedFile = objRegExp.Replace(strCompressedFile, "")
-
Set objRegExp = Nothing
-
‘Strip Spaces Greater than 2 long
-
Set objRegExp = New RegExp
-
objRegExp.IgnoreCase = True
-
objRegExp.Global = True
-
objRegExp.Pattern = "\s{2}"
-
strCompressedFile = objRegExp.Replace(strCompressedFile, "")
-
Set objRegExp = Nothing
-
Response.ContentType = "text/css"
-
Response.Write(strCompressedFile)
-
Response.Flush
-
Response.Clear
-
Case "JS" ‘Javascript
-
‘Strip New Lines
-
strCompressedFile = Replace(strCompressedFile, VbCr, "")
-
‘Strip Spaces Greater than 2 long
-
strCompressedFile = Replace(strCompressedFile, " ", "")
-
Response.ContentType = "text/javascript"
-
Response.Write(strCompressedFile)
-
Response.Flush
-
Response.Clear
-
End Select
-
End Sub
-
End Class
-
%>
Link to this post!












