Archive for May, 2007 Page 3 of 3



Unobtrusive Javascript pop-up links

One of the things I dislike the most about writing HTML is creating links that open in new windows. Since I try to make all my HTML standards-compliant, I can’t use the target="_blank" attribute in my links anymore, since it’s been deprecated in XHTML. Many have followed the new standard of making an inline onclick attribute of the link open the window, including myself. But what a pain it is to type it all out and remember the exact code to do it. Instead of this:

  <a href="http://www.example.com" onclick="window.open(this.href); return false;">External Link</a>

Wouldn’t it be easier to type this?

  <a href="http://www.example.com" rel="external">External Link</a>

I’m sure you’ll agree it’s much cleaner, easier to type, easier to remember, and to top it off, it’s (kind of) symantically correct. While the possible attribute values of rel do not include external, using it as a keyword to denote the relationship between an external page and your link makes sense. Best of all it validates with the W3C validator with flying colors.

How to Make it Happen

So how do we make the magic happen? The answer is with our good old friend Javascript. Basically we’re going to make a function that will crawl the document after the page loads and check for links that have rel="external" in it, then attach an Event.observe to each link that will cause it to open in a new window (note that you’ll need Prototype.js for this to work).

Here’s the code:

  function external_link(link) {
    var el = Event.element(link);
    if (el.tagName == 'IMG') {
      if (el.parentNode.tagName == 'A') {
        var el = el.parentNode;
      } else {
        return false;
      }
    }
    window.open(el.href);
    Event.stop(event);
    return false;
  }
  function init() {
    var links = $A(document.getElementsByTagName('a'));
    links.each(function(link) {
      if (link.getAttribute('rel') == 'external') {
        Event.observe(link,'click',function(e){ external_link(e); },false);
      }
    });
  }
  Event.observe(window,'load',init);

Note in the external_link function it checks if the item clicked was an image tag. If it is, then it will look up one node in the DOM tree to see if the image was enclosed in an anchor tag, and attach the event to that instead (since it contains the href it needs to open). If it doesn’t find anything, it will exit quietly.

Load this in your document header and off you go. You should be able to add rel="external" to any link on your page and it will open in a new window. Remember you need a copy of Prototype loaded as well. Leave a comment if have any questions or improvements.

What to do in Portland while you’re at RailsConf 2007 (updated for 2008)

If you’re attending RailsConf this year and are from out of town, you might be like me when you’re in another city: I don’t really find much outside of the touristy areas, or what’s immediately around where I’m staying. But you’re in luck! I live here in Portland, Oregon and I have a list of places to go and things to do that I think are quintessential Portland.I’ve tried to pick places that are close to the convention center, but most places will require either taking Tri-Met bus, light rail or street car, or driving. Each listing has a link to directions from the convention center so you can see about how far it is.

Good places to eat lunch or dinner
The immediate area around the convention center is mostly places like Denny’s and Red Robin. I wouldn’t recommend eating there. Here are some places for getting some good food.
Good places to eat breakfast
One thing Portlanders know how to do right is make a tasty breakfast. Check out these places if you have time to get up early and eat before the conference starts for the day.
Go see a movie, and have a beer
Portland, as far as I know, is one of the very few cities that have so many movie theatres that serve beer and food. AND to top it off, most movies are $3-$6 admission. Check out one of these theatres for a magical movie and beer experience.
Coffee Shops Galore
Portland has no shortage of coffee shops. Try not to go to Starbucks and find one of these local coffee shops.
Happy Hours
Come 4pm you’ll be wanting you some drinks. There are plenty of happy hours around town, but here are a few recommended.

Find more Portland Happy Hours at Unthirsty.com

Powell’s/Powell’s Technical
Powell’s Books is one of the largest book stores in the world, and to make it even better, they have a separate store that only sells technical books (I picked up my Programming Ruby book there, among others).
Places to find gifts for your Significant Other
You might get in trouble if you go to a conference out of town and not bring back a gift. Luckily for you there are a couple areas you can check out that have unique gifts.
Relive the golden age of arcade games
The only place in Portland that has all the classic arcade games, beer, wine and DJs you can take.
See a live band
Every night you’ll find tons of bands playing in lots of clubs around Portland. Check out the Portland Mercury before you come, or pick one up while you’re here. You find all the band listings, as well as other stuff to do while you’re here. Listed below are some recommended venues.

If you know a great place to eat, drink or have fun in Portland, leave a comment below with a suggestion or link to your list. See you at the conference.