Saturday, 19 November 2011

Using the Microsoft CDN or Google CDN for jQuery

As we develop new websites and try to introduce more user interactivity and animation effects it will become more frequent that we use some kind of JavaScript library.

Probably the most popular and most well-known is the jQuery library.  This post is going to show you how to link the library to your site.  We’ll also add a tip or two along the way.

For a jQuery tutorial click here for the w3schools page.

Adding the jQuery Library to Your Pages

The jQuery library is stored as a single JavaScript file, containing all the jQuery methods. It can be added to a web page with the following mark-up:

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

Remember that the <script> tag should be inside the page's <head> section. 

The above method requires you to download the jQuery file (see here) and store it on your server with your site.  Two versions of jQuery are available for downloading: one minified and one uncompressed (for debugging or reading).

Alternatively you can use a Content Delivery Network (CDN).  There are three CDNs available; jQuery CDN (via Media Temple), Google Ajax API CDN and the Microsoft CDN.

Google:
<script type="text/javascript" src=http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js>
</script>


Microsoft:
<script type="text/javascript" src="http://ajax.microsoft.com/ajax/jquery/jquery-1.4.2.min.js">
</script>

jQuery:
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.min.js”></script>

 

A couple of tips:

Monday, 31 October 2011

Variables in CSS File (Dynamic CSS)

Problem: Using CSS Variables/Dynamic CSS;

Firstly, I am aware that using variables in CSS seems to be topic for many a forum and blogs alike, W3C have not to my knowledge implemented variables in full within CSS as it breaks the segregation between Stylesheets and logic/script files (php etc).
Using variables is CSS has been somewhat of a minefield with entire articles and dedicated web pages on the matter. Not that these are bad in anyway, as they are, in the author's opinion, a way of producing the desired result. It is just my opinion which says these methods are, at time, very long winded and are quite 'fatty'.
Most requests for such variables in CSS are based around outputs from database queries, corporate colour schemes was my driving force and seems to be the case for the majority of reasons for the CSS variable request.
What i aim to provide with the solution i found, is a very lean and simple to use way of using variables within CSS styling.

Solution:
I come back and edit this in the near future and give you a working example of my colour scheme mySQL database table, create script and a site i used to generate the perfect tones that i required, but for now, this is the workings of "PCSS" (php CSS file).
  • Create a php file called PCSS.php
  • Paste this code into the file:
1:  <?php 
2:
3: $PCSS = getPCSS();
4: header("Content-type: text/css");
5: echo $PCSS;
6:
7:
8: ?>

  • Create a function called "getPCSS()" - Remember to insert this before the "?>" php closure tag.
  • Within the getPCSS function, write your db extract routine or variables you wish to dynamically use within a CSS file.
  • Save and close the file.
  • Open and edit the file that you wish to include the dynamic CSS within and point a new link to the new PCSS.php file (example: )
  • Thats it!
I said it wasn't going to be difficult, and in my opinion, this is the leanest way of producing the desired result.
The simple "header("Content-type: text/css");" tells the PHP file to return the content as CSS, which means you can have functions, variables, db queries etc within this file, but the return of function "getPCSS()" will pass CSS to the browser.

I could give you a bench mark, but to be honest, the results are subjective to the size of your PCSS.php file, subsequent db queries etc and performance will vary. However, with that said, an average time for my PCSS.php file to query 30 colours from 1 table, build 15 different CSS names/classes for each colour, which is around 1200 LOC, takes around 200ms.

As said, i will give you a working db example using a colour scheme which is the most common reason to require dynamic CSS (I didn't want to remember the 30 different colours and shades HTML colour codes).


If you think my post is good, or have any questions, please feel free to drop me a comment and I will get back to you.

Aaron Harrison - New Blog Contributor

Hello World!

I have recently been working with Matthew Sims over at zypher.co.uk and was asked if I wanted to contribute on their blog. I thought sure, sounds good and its probably long overdue to give back to the web community what I have taken from it over the years.

I have an array of solutions for everything from simple Excel problems/quirks, VBA solutions utilizing Windows APIs, Microsoft Word solutions/automation's, right through to solutions for web development problems which have stumbled many developers.
This means, you should begin to see various posts from myself, as well as the posts from Matt.

Just a heads up, I will be creating a post about a solution I created for "Dynamic CSS" files, i.e using php variables etc within a CSS file. I hope to get it posted today so keep your eyes peeled ;-) (here it is)

Well, enough about me. You should check-out zypher.co.uk, they have services and solutions for many ICT requirements and could be just what your looking for. Matt is a very knowledgeable professional with a vast and dynamic background which brings a wealth of experience.


Hope you find my posts easy to follow, useful and informative. If you have any questions or feedback (please, be gentle), feel free to leave a comment.

Wednesday, 31 August 2011

Creating and reading data from text files

It is sometimes useful to keep data in a text file.  For example one of our clients has a resource file which has onOpen and onClose routines that log the current user, the date and time and whether the file was opened read/write or read only.

The code below shows how this can be done.

Sub Create_A_File()
' Put data into a text file
' This routine will create the file if it does not already exist

    Dim strMsg  As String
    Dim strFile As String

    strFile = ThisWorkbook.Path & "myFile.txt"
    ' could be .doc, .txt, .log etc

    strMsg = "Some text could go here"

    ' Open file for output - clears any current data
    'Open strFile For Output As #1
    ' or Open file for append - updates any current data
    Open strFile For Append As #1

    Print #1,                       ' Print blank line to file.
    Print #1, "*****"               ' Print ***** line to file.
    Print #1, strMsg                ' Print a variables value to the text file
    Print #1, Now, strMsg           ' Print date/time and the message, using commas create tab seperated data
    Print #1,                       ' Print another blank line to file.
    Print #1, Application.UserName  ' Print the current user's Excel username
    Print #1, Now                   ' Print the date and time

    Close #1                        ' Close file.

End Sub

Sub GetFromTxt()
' Copy data from one text file to another

    Dim FilePath    As String
    Dim File1       As String
    Dim File2       As String
    Dim File3       As String
    Dim FileData    As String

    Const FileExt   As String = ".txt"

    FilePath = "C:\File\Path\"
    File1 = "File Name 1"
    File2 = "File Name 2"
    File3 = "File Name 3"

    ' Example: FilePath & File1 & FileExt = "C:\File\Path\File Name 1.txt"

    'Open all relevant files
    Open FilePath & File1 & FileExt For Output As #1
    Open FilePath & File2 & FileExt For Input As #2
    Open FilePath & File3 & FileExt For Input As #3

    'Copy each line of the first existing file into the
    'new combined file
    Do While Not EOF(2)
        Input #2, FileData
        Print #1, FileData
    Loop

    'Copy each line of the second existing file into the
    'new combined file
    Do While Not EOF(3)
        Input #3, FileData
        Print #1, FileData
    Loop

    'Close all files
    Close #1
    Close #2
    Close #3
End Sub

Hope that proves useful.

Friday, 26 August 2011

Useful VBA file functions - Microsoft Scripting Runtime

Some example functions for finding out the last modified date of a file, copying a file and deleting a file using VBA.

Public Sub File_Last_Modified_Date()
' Use a function to return the last modified date of a file
    MsgBox f_File_Last_Modified_Date("C:\Put\the\full\file\address\here.xlsx")
End Sub

Private Function f_File_Last_Modified_Date(FileName As String) As String
' In the VBE, set a reference to Microsoft Scripting runtime
' Tools -> References... ->
    Dim fso As Scripting.FileSystemObject
    Dim fsof As Scripting.File
    Dim strPath As String

    Set fso = New FileSystemObject
    strPath = FileName
    Set fsof = fso.GetFile(strPath)
    With fsof
        f_File_Last_Modified_Date = .DateLastModified
    End With

    Set fso = Nothing
    Set fsof = Nothing
End Function


Public Sub CopyFile()
' Copy a file, the file cannot be open when the copy is attempted

    Dim SourceFile As String
    Dim DestFile As String

    SourceFile = "C:\Put\the\full\file\address\here.doc"
    DestFile = "C:\Put\the\new\file\address\here.doc"

    FileCopy SourceFile, DestFile
End Sub

Public Sub DeleteFile()
' Delete a file

    Dim FileName As String

    FileName = "C:\Put\the\full\file\address\here.txt"

    ' Check the file exists
    If Dir(FileName) = "" Then
        ' File does not exist
    Else
        ' Delete the file
        Kill FileName
    End If

End Sub

As always, any questions or comments please let us know via the comments section.

Monday, 22 August 2011

Application.Filesearch Replacement For Office 2007

If you have ever used completed a search for files using VBA you probably made use of Application.Filesearch.  However, if you’ve updated to Office 2007 or 2010 you may have noticed that Filesearch has been removed… but the (very) old Dir remains, so you can use this instead, an example of which follows:

Sub FileSearch()
    Dim fso As Object
    Dim FileName As String
    Dim strArr(1 To 65536, 1 To 1) As String
    Dim i As Long

    ' Set the directory / filename you are looking for
    Const strDir As String = "C:\Your\File\Path"
    Const SearchTerm As String = "YourFileNameTerm"

    ' Complete the search
    Let FileName = Dir$(strDir & "\*" & SearchTerm & "*.xls")
    Do While FileName <> vbNullString
        ' For each file found load to the array
        Let i = i + 1
        Let strArr(i, 1) = strDir & "\" & FileName
        Let FileName = Dir$()
    Loop

    ' Search within sub-folders
    Set fso = CreateObject("Scripting.FileSystemObject")
    Call RecurseSubFolders(fso.GetFolder(strDir), strArr(), i, SearchTerm)

    ' Tidy up and copy the results to the active worksheet
    Set fso = Nothing
    If i > 0 Then
        Range("A1").Resize(i).Value = strArr
    End If

End Sub

Private Sub RecurseSubFolders( _
    ByRef Folder As Object, _
    ByRef strArr() As String, _
    ByRef i As Long, _
    ByRef SearchTerm As String)

    Dim SubFolder As Object
    Dim FileName As String

    ' Search sub folders
    For Each SubFolder In Folder.SubFolders
        Let FileName = Dir$(SubFolder.Path & "\*" & SearchTerm & "*.xls")
        Do While FileName <> vbNullString
            Let i = i + 1
            Let strArr(i, 1) = SubFolder.Path & "\" & FileName
            Let FileName = Dir$()
        Loop
        Call RecurseSubFolders(SubFolder, strArr(), i, SearchTerm)
    Next
End Sub

Tuesday, 12 July 2011

DreamWeaver - while executing onload in _onOpen.htm, the following JavaScript error(s) occurred:

 

DreamWeaverJsError

To fix the above error navigate to one of the file address below:

Windows XP:

C:\Documents and Settings\Administrator\Ap Data\Adobe\Dreamweaver CS5\en_US\Configuration\

Vista:

C:\Users\[Your Username]\AppData\Roaming\Adobe\Dreamweaver CS5\en_US\Configuration\

Windows 7:

C:\Users\[user]\Ap CS4\en_US\Configuration\

And delete the file called ‘WinFileCache-[random numbers].dat’.