Thursday, July 23, 2009

Javascript Cookies

Thanks to Jason McCreary, I was able to get Javascript cookies working very easily.

The one thing missing on that page, though, is a more general bit of help with how to safely use cookies. My first attempt (below) almost worked perfectly, however there was a little bit of weirdness.


Cookie.init({name: "macBlogMenu"});
open_nodes = Cookie.getData("open_nodes");
<do stuff>
Cookie.setData("open_nodes",open_nodes);


I am working on a rails site which is defined such that /blog shows the current blog and /blog/N shows the blog with id=N. Since both of these show a blog entry and the cookie was to keep track of menu state on the blog display page, I used a javascript cookie with just default options. This was a bad idea. State was not being updated when I looked at other blog entries, only when I used the default (current) blog entry.

Anyone familiar with cookies would probably already know the answer is to define the path option for the Cookie object. So the better code is:


Cookie.init({name: "macBlogMenu"});
Cookie.options.path = "/blog/";
open_nodes = Cookie.getData("open_nodes");
<do stuff>
Cookie.setData("open_nodes",open_nodes);


The path needs to be "/blog/" because of the scope rules for cookies. Documents at root (one level - i.e. /blog), can read and change cookies for documents on lower nodes. Lower nodes (/blog/3) can read cookies defined for higher level paths, but not set them. By defining the path this way, it is accessible to /blog and /blog/N documents.

Wednesday, July 15, 2009

Git Diff and Empty Files

I use git for local source control whenever possible.

If I am working with a system which does not have access to my repository, sometimes I use "git diff" to create patch files to update the remote site.

Today, I found a huge flaw in that strategy. If I have an empty file that was created on my local site, the "git diff" output does not include the file at all!

There are two ways around this. The first is to remember that the file needs to be created on the remote system and to "touch" the file. The second, and better way, is to never create completely empty files. Instead, create a file with a comment that it is intentionally left empty.

Friday, July 10, 2009

Jackalope Apache Virtual Hosts

I had an annoying problem with my Apache virtual hosts when I updated to Juanty Jackalope recently. Any hostname I used to access my server was resulting in the same virtual host serving the page. I lived with it for a week or so by using a2dissite/a2ensite to only enable the virtual host I needed at the time (obviously a poor solution).

I figured it out today. The trick was to go through all of my virtual hosts and change the line:


<VirtualHost *>

to

<VirtualHost *:80>

I hope this helps someone.