Use one bookmark to load different versions of a site

Mar 10, '10 07:30:00AM

Contributed by: TomHopper

I have several sites that I access on both my iPhone and desktop, so I like to have quick access to them via the Bookmarks Bar. However, these sites have different versions for the desktop, for mobiles, and, in some cases, yet another version for the iPhone. Usually, the full version doesn't work so well on the iPhone, and the iPhone version is undesirable on the desktop.

Instead of creating a plain bookmark, a little Javascript can make a bookmark context-sensitive, and allow you to have one bookmark that opens the right version of a page, depending on which platform you're browsing from. The basic idea is to use some client-side Javascript to check the browser's platform (a.k.a. operating system), and then tell the browser to access a URL based on that check. The code looks like this:

javascript:if((navigator.userAgent.match(/iPhone/i))||(navigator.userAgent.match(/iPod/i))){location.href='URL1'}else{location.href='URL2'}
For Google Translate, which has a nice iPhone version if you open the right URL, but a stripped-down mobile version if you just go to translate.google.com on your iPhone or iPod, this looks like
javascript:if((navigator.userAgent.match(/iPhone/i))||(navigator.userAgent.match(/iPod/i))){location.href='http://www.google.com/m/translate?hl=en&source=mobilesearchapp'}else{location.href='http://translate.google.com/#auto|en'}
To create this bookmark, copy the above code, create a new bookmark with the Bookmarks » Add Bookmark command, paste the code into the Address field, and give your bookmark a memorable name. Once you have one bookmarklet, you can make copies that work with the sites of your choice. Note that the URLs have to be enclosed in single quotes ('), and that spaces and line breaks will probably break the bookmarklet.

Using bookmark-syncing software, you should be able to use these bookmarklets in other browsers and on other operating systems.

Comments (3)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20100308060401535