Saturday, January 23, 2010

HTML emails

HTML emails can be hard to create since most of the tricks used to develop web pages are completely unusable when creating HTML emails. CSS is not well supported, images can be a disaster, and layout is best done using tables (gasp). describes the situation quite nicely: "Think back to the old days of the web browser wars. When Netscape and Microsoft were duking it out. There was no Flash plugin. It was only shockwave. Put away your DIVs and DHTML. Dust off those tables, shim.gifs, and font tags, because you’re gonna need 'em."

Webmail services

Webmail services strip the <html> <head> and <body> tags from an HTML email. Keep this in mind when coding emails.

Email width

Since most people view emails in a reading pane rather than full screen, set the email width at:

500-600 pixels


Images are not displayed immediately in all email clients (to name a few: Outlook 2003, Outlook Express, AOL, Gmail, Thunderbird) . The user has to allow the email client to display images before they are shown. Since this is the case, do the following when using images in emails:

  • Enter a height and a width in the image tag to act as a placeholder
  • Use alternative text (alt="")
  • Don't use images for important content
  • Don't use background images. They are not displayed by all email clients.
Javascript, ActiveX, Flash, and embedded movie files

Don't even think about it.


Don't use div tags to position elements since some email clients strip div tags. Using tables for layout is probably the best way to create a well-formed email. suggests using separate tables for the header, body, and footer of emails since some email clients do not handle colspan (Lotus Notes).

As noted above, Webmail services strip the <body> tag from an email. If you have a background color applied to the body tag, this will get stripped as well. Use a 100% wide table to hold your email and apply the background color to the table.


There is no standard support for CSS in today's email clients. No email clients allow for external style sheets, and, since Webmail services strip the portion of the email, it is best to place any style within the body of the email. In addition, CSS positioning is not supported by email clients.

To keep email style consistent throughout all email clients, review the following table :

Email ClientSupports…
Yahoo! MailCSS
Outlook and Outlook ExpressCSS
GmailInline CSS
Lotus NotesInline CSS
Mac MailCSS
Eudora for the MacNo CSS


Email clients to test:
  • Web: Hotmail, Yahoo! Mail, Yahoo! Mail Beta, Gmail
  • PC – Outlook, Outlook Express, AOL 9, Thunderbird, Lotus Notes
  • Mac – Mac Mail, Entourage, Eudora
  • Inline CSS is most widely supported
  • Don't use CSS to position elements
  • Use tables for layout (maybe even use separate tables for header, body, footer)
  • If applying a background color, wrap the whole email in a 100% width table
  • Test in all email clients before sending your email!
Where I Got My Info

Monday, January 11, 2010

Source Code Formatter

Just wanted to send out a quick thanks to the developers of the source code formatter tool I used to format the code in my previous post! It's very helpful; I'm sure I'll be using it many times in the future! Check it out here:

Sunday, January 10, 2010

How to Create a Bookmarklet Like FriendFeed's


I've been working on a gift web site called Gifty with my fiance, James. The site allows users to create groups, such as a family group. The user can then add their gift wishlist to the group. Other users in the group can see the person's wishlist and select which gifts to get that person.

I thought a great addition for the site would be a bookmarklet similar to FriendFeed's. The user would be able to add a gift from any site they're on. For example, if they're on the Amazon Kindle page, when they clicked on the bookmarklet, they would be able to add the Kindle to their wishlist.

Before I began, I had to figure out how FriendFeed did it. I encountered a couple surprises along the way, and thought I'd share with everyone the details of the code.

The Concept

The concept is pretty simple:
  1. Create a link that is purely a JavaScript method call, which acts as the bookmark
  2. Have this JavaScript method add a div element to any page using JavaScript
  3. Display a form in this div element
  4. Have the form submit a post to a URL on Gifty
  5. Remove the div element from the page.
Seems easy, right? Well, yes, the concept is easy, but there are a few tricky aspects that I didn't foresee. I'll go through each step, describing how I (and FriendFeed) accomplished the goal.

1. Create a link that is purely a JavaScript method call, which acts as the bookmark

This step was very simple. Drag the following link to your bookmark bar and test it out:


You should see a JavaScript alert pop up, while you remain on the same page. (If nothing happens, you might have JavaScript disabled.) What is the code behind this link? It's simply


Now, I wanted to do something more complex than a simple alert, of course, so I reviewed FriendFeed's bookmark JavaScript, which is as follows:


As you can see, their function creates a new script element with their JavaScript file as the source and appends this to the page. How clever! All updates to the code can be changed in their file. As long as the link to the JavaScript file remains the same, new versions will be pushed automatically to all users.

I replaced FriendFeed's src file with my own, bookmarklet.js, and was on my way to the next step.

2. Have this javascript method add a div element to any page using javascript

Adding the div tag to the page is pretty simple, too. I applied a similar approach to FriendFeed, and used the following code:

 var container = document.createElement("div"); = "0"; = "0"; = "1px solid #000000"; = "giftybox"; = "absolute"; = "0"; = "0"; = 100000; = "350px"; = "210px"; = "white";

A new document element is created and all the style is applied to the element. Important style attributes to note are the absolute positioning and the z-index. The absolute positioning allows the element to be placed exactly where we ask it to be placed on the page, in spite of any other elements that might be in the way. The z-index means that this element should be placed at a position of 100,000 above any other elements. This takes an assumption that all other HTML elements on the page have a z-index smaller than this. If there's an element with a z-index of 100,000+, then that element will display in front of my div. However, I'm willing to take the bet that most HTML elements will not have such a large z-index, and have settled with 100K.

This JavaScript is placed in the JavaScript file mentioned above. To make sure my JavaScript didn't interfere with any other JavaScript on the page, I used namespaces. I created a new variable called giftyFunctions and set this equal to an anonymous function with the return value being a map of the function name to another anonymous function. Confused? Here's what it looks like in the code:

 var giftyFunctions = function() {
return {
addDiv : function() {
//code from above

Now, at the bottom of the file, I can call the function to add the div element as follows:


This should, hopefully, eliminate the possibility that another function on the page would have the same name and would interfere with my function.

3. Display a form in the div element

The form was a bit trickier. The HTML had to be hosted on our own servers for two reasons:
  1. We needed to make sure the user is logged in before showing the form
  2. We needed to add user-specific data to the page (e.g., what groups they belong to)
To get around this, I looked to FriendFeed again. Again, they provided a very clever solution: they added an iframe element to the div element added earlier. The source was set to an HTML page hosted on their own servers. I applied a similar approach, creating the following HTML page:

 <body onload="addText();">
<p style="float:right;"><a href="javascript:closeBox('close');">Close</a></p>
<form id="frameform">
<p><label for="giftDescription">Gift Name:</label> <input type="text" name="_giftDescription" id="_giftDescription" /></p>
<p><label for="giftLink">Link:</label> <input type="text" name="_giftLink" id="_giftLink" /></p>
<p><label for="groupId">Group:</label> <select name="_groupId" id ="_groupId" >
{% for group in userGroups %}
<option value={{ }}>{{ group.groupName }}</option>
{% endfor %}
<input type="button" value="Submit" onclick="submitForm();" />

Note that this form does not contain an action, and the submit button is just a button with a JavaScript onclick event. This differs from FriendFeed's form, in which they use an actual submit input. I'm using a button since I want to control the submission of the form via the JavaScript function, submitForm. More about this later.

The iframe source is set to the above HTML by adding a couple lines of JavaScript to the JavaScript file:

 container.innerHTML = '<iframe style="width:100%;height:100%;border:0px;" id="giftyframe"></iframe>';

The setIFrameLocation code is as follows:

 function setIFrameLocation() {
var iframe;
if (navigator.userAgent.indexOf("Safari") != -1) {
iframe = frames["giftyframe"];
} else {
iframe = document.getElementById("giftyframe").contentWindow;
if (!iframe) return;
var url = ''
url += '#gifty?giftname=' + document.title;
url += '&giftylocation=' + window.location.href;
try {
} catch (e) {
iframe.location = url; // safari

Note, there are two parameters added to the end of the iframe URL: giftname and giftylocation. These two parameters pass important information to the iframe so that the two input fields in the form, Gift Name and Link, can be given initial values. These are added via the addText function called on page load.

 function addText() { 
var windowLocation = window.location.href;
var params = windowLocation.split('#gifty?')[1];
var subparams = params.split('&gifty');
var title = unescape(subparams[0].split('=',2)[1]);
parentLocation = subparams[1].substring(subparams[1].indexOf('=')+1,subparams[1].length);
document.getElementById('_giftDescription').value = title;
document.getElementById('_giftLink').value = parentLocation;

Now that the form is added to the div, it's time to move on to the form submission.

Have the form submit a post to a URL on Gifty

As I mentioned earlier, the form submission is performed via the submitForm method. This method uses a jQuery AJAX method to submit the form data via a post. The code is extremely simple, thanks to jQuery's help:

 function submitForm() {
$.post("/bookmarklet", $("#frameform").serialize());

The $.post function, a jQuery function, takes a URL and data as parameters. It makes an asynchronous call to the URL and posts the data to the URL. Since the call is asynchronous, it allows the user to remain on the current page. More information can be found on the jQuery website.

5. Remove the div element from the page.

Once the form is submitted, the div element needs to be removed from the page. To do so, control needs to return to the parent window, since the iframe itself can not remove the div element from the parent page. Unfortunately, JavaScript does easily allow for control to return to the parent. If the source of an iframe has a different domain from the parent, the parent property can not be used. So, what to do??

Again, I looked to FriendFeed to see their approach. Their solution was unique. In the initial JavaScript file, they set up an interval for a function that checks the page URL every 50 milliseconds. The function checks whether a message has been added to the end of the URL. The message begins with # + a FriendFeed specific string + =. Depending on the message, the function performs different actions, including removal of the div element.

I mimicked this design, and added the interval to my bookmarklet.js file:

 var interval = window.setInterval(function(){
}, 50);

The giftyFrameMessage function finds the message at the end of the URL and sends this to another function to handle messages:

 function giftyFrameMessage(){
var gCurScroll = giftyScrollPos();
var hash = location.href.split('#');
if (hash.length > 1 && hash[hash.length - 1].match('gifty') != null) {
location.replace(hash[0] + "#");
giftyHandleMessage(hash[hash.length - 1]);

Finally, the giftyHandleMessage simply closes the div element:

 function giftyHandleMessage(msg){
//close the box
function giftyClose(){
var giftybox = document.getElementById('giftybox');
window.onscroll = null;

This function can be more complex and perform different actions based on the message. However, my code required nothing more complex at this point.

Now that I had the interval set up, I had to update the URL from the iframe. The iframe doesn't have access to the parent, but it does have access to "top". After submitting the form, I called the following method:

 function closeBox(message) {
var url = parentLocation + "#gifty=" + message;
try {
} catch (e) {
top.location = url;

This changes the URL, adding the message to the end. Once the giftyFrameMessage function is called, it finds the #gifty string in it, and then closes the div.

And that's the majority of the code for the FriendFeed bookmarklet! Of course there's much more to it than this, but this will help get you started!

Saturday, January 9, 2010

Genetic Algorithms - Roulette Elitism and Sorting

I wrote this paper on Genetic Algorithms for my AI class. I tested the effects of sorting on Roulette Elitism. Based on my studies, I believe that sorting actually has a positive effect on Roulette Elitism and should be included in the algorithm. Please read my paper for more details!

My First Android Application - Device photos

This tutorial shows you the code to get metadata about the images on your Android phone and also how to set up your development environment to test the code locally using the Emulator. This tutorial assumes that you have properly installed version 1.5 of the Android SDK, are using Eclipse to develop Android applications, and have installed the ADT for Eclipse. Also, these instructions were written for Mac users. I will link to instructions for Linux users whenever I can. PC users - you're on your own.

Creating an SD Card

First and foremost: you should know that the photos you take with your Android phone are stored on the SD Card. The images are stored in the directory: /sdcard/dcim/Camera. We're going to mimic the SD Card locally. The local SD Card is actually a disk image. No SD Card disk image comes with Android SDK, so we need to create one.

To create the SD Card image, navigate to your /tools directory using the Terminal. Depending on your PATH variable, you might have to add the tools directory to your PATH. This will allow you run the files in the tools directory. In my case, I ran the command:

export PATH=$PATH:/Users/kat/JavaLibraries/android1.5/tools

Next, we need to actually create a disk image of the SD Card. Run the command:

mksdcard 1024M .img

In my case, I ran the command:

mksdcard 1024M myimage.img

If you look in the tools directory, you should see a new file in there called 'myimage.img' (or whatever you decided to call it). Please make sure you use the .img extension, since this will help you later when mounting the image. Which brings me to my next topic: mounting the image. :)

Adding Folders/Images to the SD Card Image

In order to create folders and upload files to our new image, I found it easiest to use the Disk Utility on my mac to mount the 'myimage.img' file. If you're using Linux, try the directions here.

Start the Disk Utility application (it's in the System Utilities folder in the Applications directory). Once started, Select File > Open Disk Image... Navigate to your newly created disk image in the tools directory. Select OK. You should see the disk image in the list. If it's not already opened, right click on the image and select "Open". The SDCARD should appear under it. Right-click on the SDCARD and select "Reveal in Finder". Double-click on the "sdcard" folder in the Finder window. Once in this folder, you can create new folders and copy and paste images within the folders. In our case, we want to create the following directory structure under sdcard:


Within the Camera folder, place some images. JPGs should work fine. Not too sure about any other formats, I haven't tried them yet.

Finally, we need to unmount the disk image. Right-click on the SDCARD in the Disk Utility window. Select 'unmount sdcard'. Then eject the disk image by right-clicking on it and select "eject".

Perfect, we're almost done! Not too difficult, right?

Setting up Eclipse to Recognize the New SD Card Image

Next step is to set up Eclipse to run our new sdcard image with the Emulator instead of the non-existent one it uses by default. There are 2 possible places to make this change in Eclipse. I found one doesn't work at all, the other does. I'm going to include both places, though, just in case.

The one that didn't seem to work - In Eclipse, click on the Eclipse Menu > Preference. Expand the Android menu, select Launch. In the Default emulator options, enter

-sdcard /[path]/[imagename].img

In my case, I entered:

-sdcard /Users/kat/JavaLibraries/android1.5/tools/myimage.img

The one that did work - In Eclipse, create an Android project titled 'PhotoFun' (or whatever). Right click on the Project, select Run As > Open Run Dialog... In the Run Dialog Box, under the "Target" tab, enter the same text as above (-sdcard /[path]/[imagename].img) into the Additional Emulator Command Line Options box.


If you didn't create the Android Project as I mentioned in Step 2 directly above, please do so now. Call the Project 'PhotoFun' or whatever you want.

First, open the res > layout > main.xml file. Replace the TextView element with the following:


Next, open the main class (src > Add the following lines to the onCreate method after setContentView(R.layout.main);

TextView imageData = (TextView) findViewById(;

String[] s = {MediaStore.Images.Media.TITLE,
MediaStore.Images.Media.DATE_MODIFIED, MediaStore.Images.Media.SIZE};
Cursor cursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, s, null,null,null);

StringBuilder string = new StringBuilder();
while(cursor.moveToNext()) {
String theFile =
"Title: " + cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.TITLE))
+ "\n" +
"Date Mod: " +
+ "\n" +
"Size: " +

string.append(theFile + "\n\n");


Now run your application as a Android app. Once the Emulator is loaded you should see a list of all the photos in the /sdcard/dcim/Camera directory! Awesome!

Hello, World

OK, yea, the title to this post is lame and much overused, but I'm using it! This is my first post in, hopefully, a series of weekly (biweekly? monthly?) posts about programming. I'm a second year masters student at the University of San Francisco, studying web sciences. I've learned a lot during my studies and would like to share some of this knowledge with the rest of the world!

Thanks for stopping by!