Archive for the ‘Tech’ Category

Why use Google hosted jQuery

Thursday, March 4th, 2010

Here are the benefits of using the Google hosted jQuery libraries:

  • By using the Google hosted jQuery libraries, since many applications use the Google infrastructure, when someone comes to your application, the Google hosted files may already be loaded into the visitor’s browser cache.
  • The subtle performance issue associated with the transfer of cookie data is revolved because your domain  is different from Google’s  (i.e.,: not google.com), so no cookie data or other verbose headers will be sent up, saving precious bytes.
  • By loading the jQuery libraries from Google, the HTTP requests for those libraries don’t compete with the requests for assets from your domain, speeding up the overall page load time:
    “Browsers typically limit the number of concurrent HTTP connections per server to be 2. If a browser session has two HTTP connections already [for a given server], any further connection requests will have to wait until one of these two HTTP connections is finished.” — Source: OpenAjax Alliance
  • Google hosted files are compressed using GZIP.
  • Google has a distributed CDN at various points around the world, so the files are “close” to the visitor.
  • Google servers are fast.

Here’s the drawback:

  • In the highly unlikely event that Google’s CDN goes down, the missing jQuery libraries will prevent your jQuery dependent page from loading correctly. The fix for this is to alternatively load the jQuery library from somewhere else. Microsoft now hosts the jQuery library on their CDN.

Here’s an example of alternate loading. Try loading from Google. Check for the presence of the jQuery function. If it isn’t there, load jQuery from Microsoft.

<script src=”http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js” type=”text/javascript”></script>
<script type=”text/javascript”>
if (typeof jQuery !== ‘function’) {
    document.write(‘<scri’ + ‘pt type=”text/javascript” src=”http://ajax.microsoft.com/ajax/jquery/jquery-1.4.2.min.js”></scri’ + ‘pt>’);
}
</script>

If both Microsoft and Google are down, then the world must be at an end, so your site not loading is the least of your problems.

iPhones can do anything!

Tuesday, May 12th, 2009

I just downloaded the free Wordpress iPhone App, from the iTunes App store, and within two minutes I was writing this post. I’m having a vision of a new world, in the not too distant future, where us humans will evolve to have sharper finger tips, and really great close up vision (within 6-7 inches from the eyes).

Is the future of personal computing going to be held in your pocket? Are we going to give up the big and clunky desktops and their sleeker cousins, the laptops altogether? We can already do pretty much everyting from our palms.

Good job Apple ! Keep it up.

SSH and SCP with keys instead of passwords

Tuesday, June 10th, 2008

I’m writing about this bit of useful shell scripting, mostly so I know where to find it when I might need it again.

I needed to set up automatic backups using a shell script in Mac OS X, to copy (scp) tarballs from a mounted network drive on my Mac to a remote Linux environment. The only problem was the ssh authentication — the remote Linux server seemed to want a password every time. I remembered a friend of mine had done this a while back, and I started digging around. Here’s the summary of how you can set up your machine to be recognized by the remote machine,

1. On my Mac (Leopard), using the Terminal App, I ran,

ssh-keygen -t rsa

This outputs the following,

Generating public/private rsa key pair.
Enter file in which to save the key
(/Users/[username]/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):

I left that blank,

Enter same passphrase again:

Left that blank too,

Your identification has been saved in
/Users/[username]/.ssh/id_rsa.
Your public key has been saved in
/Users/[username]/.ssh/id_rsa.pub.
The key fingerprint is:
... finger print string here ...
[username]@[machine].local

Basically, this created two files in my user folder’s .ssh/ folder,

.ssh/id_rsa (This is the private key — guard it well)
.ssh/id_rsa.pub (This is the public key — pass it along)

2. I SSHed into the remote server to make sure my user account’s folder over there contained a .ssh/ folder. It didn’t, so I created one,

SSHed into remote server:

ssh user@remote

I was prompted for my password,

user@remote's password:

Once I was in, I created the .ssh/ folder,

mkdir .ssh

Then I exited the remote server.

3. Then I had to transfer my public key over to the remote server. I ran the following from my home folder,

ssh user@remote "cat >> .ssh/authorized_keys"
< .ssh/id_rsa.pub

Again, I was prompted for my password,

user@remote's password:

When it was done adding my public key, it logged me out.

Now when I run,

ssh user@remote

I no longer get prompted for a password, I just go right in. This means my backup script, using scp, can run as a cron job.

ZIP Code Nerdiness

Tuesday, October 2nd, 2007

I’ve been playing around with some web services, trying to gather ZIP code data, but quickly realized that it would take forever to gather up all of the US ZIP codes—there are just under 80,000 of them. So I bit the bullet and bought a ZIP code database.

With that, I created a simple ZIP code look-up tool, which displays the city, state, latitude, longitude, etc. for any given ZIP code.

I got even more nerdy, and added a distance calculator tool that gives you distance between two ZIP codes. It’s using the haversine formula, which gives the great-circle distances between two latitude/longitude point.

Here’s are the PHP functions that I use to do the calculation:

function haversine($lat1, $long1, $lat2, $long2, $earth){
  //Point 1 cords
  $lat1 = deg2rad($lat1);
  $long1= deg2rad($long1);
  //Point 2 cords
  $lat2 = deg2rad($lat2);
  $long2= deg2rad($long2);
  //Haversine Formula
  $dlong=$long2-$long1;
  $dlat=$lat2-$lat1;
  $sinlat=sin($dlat/2);
  $sinlong=sin($dlong/2);
  $a=($sinlat*$sinlat)+cos($lat1)*cos($lat2)*($sinlong*$sinlong);
  $c=2*asin(min(1,sqrt($a)));
  $d=round($earth*$c);
  return $d;
}
function getMiles($lat1, $long1, $lat2, $long2){
  return haversine($lat1, $long1, $lat2, $long2, 3960);
}
function getKilometers($lat1, $long1, $lat2, $long2){
  return haversine($lat1, $long1, $lat2, $long2, 6371);
}