RSS
 

Posts Tagged ‘vbscript’

Optimizing Scripts

22 Jan

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!

  1. <!–#include virtual="/includes/server/classes/compress.asp"–>
  2. <%
  3. Set objCompress = New clsCompress
  4.         Dim FilePath : FilePath =       Split(Request.QueryString("file"), ",")
  5.         objCompress.strFile =           FilePath
  6.         objCompress.ReturnScripts()
  7. Set objCompress = Nothing
  8. %>

{please note, you will have to change the location of the include file.}

Include File

  1. <%
  2. Class clsCompress
  3.         Public strFile
  4.         Private objFSO, objFile, objRegExp, strCompressedFile, strFileType
  5.         Private Sub Class_Initialize()
  6.                 strCompressedFile = ""
  7.         End Sub
  8.         Public Sub ReturnScripts()
  9.                 Set objFSO = CreateObject("Scripting.FileSystemObject")
  10.                         If IsArray(strFile) Then
  11.                                 For i = 0 To UBound(strFile)
  12.                                         Set objFile = objFSO.OpenTextFile(Server.MapPath(strFile(i)))
  13.                                                 If Not(objFile.AtEndOfStream) Then
  14.                                                         strCompressedFile = strCompressedFile & VbCrLf & objFile.ReadAll
  15.                                                 End If
  16.                                         Set objFile = Nothing
  17.                                         tmp = Right(strFile(i), 3)
  18.                                         strFileType = Replace(tmp, ".", "")
  19.                                 Next : i = Null
  20.                                 Erase strFile
  21.                         End If
  22.                 Set objFSO = Nothing
  23.                 Select Case UCase(strFileType)
  24.                         Case "CSS" ‘CSS
  25.                                 Set objRegExp = New RegExp
  26.                                         objRegExp.IgnoreCase = True
  27.                                         objRegExp.Global = True
  28.                                         objRegExp.Pattern = "/\*[^*]*\*+([^/][^*]*\*+)*/"
  29.                                         strCompressedFile = objRegExp.Replace(strCompressedFile, "")
  30.                                 Set objRegExp = Nothing                        
  31.                                 ‘Strip Line Breaks
  32.                                 Set objRegExp = New RegExp
  33.                                         objRegExp.IgnoreCase = True
  34.                                         objRegExp.Global = True
  35.                                         objRegExp.Pattern = "\n+"
  36.                                         strCompressedFile = objRegExp.Replace(strCompressedFile, "")
  37.                                 Set objRegExp = Nothing                        
  38.                                 ‘Strip Tabs
  39.                                 Set objRegExp = New RegExp
  40.                                         objRegExp.IgnoreCase = True
  41.                                         objRegExp.Global = True
  42.                                         objRegExp.Pattern = "\t+"
  43.                                         strCompressedFile = objRegExp.Replace(strCompressedFile, "")
  44.                                 Set objRegExp = Nothing
  45.                                 ‘Strip Spaces Greater than 2 long
  46.                                 Set objRegExp = New RegExp
  47.                                         objRegExp.IgnoreCase = True
  48.                                         objRegExp.Global = True
  49.                                         objRegExp.Pattern = "\s{2}"
  50.                                         strCompressedFile = objRegExp.Replace(strCompressedFile, "")
  51.                                 Set objRegExp = Nothing
  52.                                 Response.ContentType = "text/css"
  53.                                 Response.Write(strCompressedFile)
  54.                                 Response.Flush
  55.                                 Response.Clear
  56.                         Case "JS" ‘Javascript
  57.                                 ‘Strip New Lines
  58.                                 strCompressedFile = Replace(strCompressedFile, VbCr, "")
  59.                                 ‘Strip Spaces Greater than 2 long
  60.                                 strCompressedFile = Replace(strCompressedFile, "  ", "")
  61.                                 Response.ContentType = "text/javascript"
  62.                                 Response.Write(strCompressedFile)
  63.                                 Response.Flush
  64.                                 Response.Clear
  65.                 End Select
  66.         End Sub
  67. End Class
  68. %>
 
 
 

Optimized by SEO Ultimate