Archive for April, 2009

Open external links in new window

Tuesday, April 21st, 2009

One of the most common complaints from my boss is “how come links to external sites don’t open in a new window” and my response is usually a shrug. There are many reasons why a link may appear on a web page… A developer could forget to add the target attribute when inserting it. A content writer could be using a WYSIWYG editor that doesn’t support it. The content might be bulk loaded without proofreading for the target attribute. It’s tough to control these things (i.e. ensure that the target=”_blank” attribute is set).

I’ll ignore the religious argument over allowing users to decide how they want to open links. The person who signs my paycheck asked for this.

I wrote a Javascript function to parse out all hyperlinks on a page and set the target attribute to _blank when the href appears to be different that the current page’s host/domain. It ignores hrefs that contain or begin with “javascript:” so that a new window doesn’t open just to call a Javascript function.

This function uses Prototype, so you’ll have to download that in order to use this (or you’ll have to tweak this to not use prototype).

function externalLinksInNewWindow(){
    if (!document.getElementsByTagName)
        return;
    var anchors = document.getElementsByTagName("a");
    for (var i = 0; i < anchors.length; i++) {
        var anchor = anchors[i];
        var thisHost = location.host;
        // anchorHost is http://www.someothersite.com/somepath
        var anchorHost = anchor.href;

        // if anchorHost starts with http, parse it
        // if it doesn't start with http (e.g. ftp://) then don't bother parsing
        if (anchorHost.indexOf('http') == 0) {
            // this makes anchorhost like www.someothersite.com/somepath
            anchorHost = anchorHost.substring(anchorHost.indexOf('//') + 2, anchorHost.length);
            // this makes anchorhost like www.someothersite.com
            if (anchorHost.indexOf('/') > 0)
                anchorHost = anchorHost.substring(0, anchorHost.indexOf('/'));
        }

        // allow for using href="javascript:myFunction()" instead of onclick="myFunction()"
        if (anchorHost.indexOf('javascript:') == -1 && anchorHost != thisHost){
            anchor.target = "_blank";
        }
    }
}

Event.observe( window, 'load', function() {
    externalLinksInNewWindow();
} );

MSI VR705 Notebook

Thursday, April 16th, 2009

My new laptop arrived today from Newegg. It is a MSI VR-705 notebook in their value series. Talk about specs: 2.0GHz Core 2 Duo, 4GB RAM, 17″ widescreen, HDMI out, webcam, SD card reader, and full keyboard with a real numeric keypad (never seen an actual numeric keypad in a laptop, just the silly one where the 789UIOPJKLM keys double as a keypad [does anyone actually type numbers that way?]). With all that built-in, I don’t even know what I could plug into the USB ports!!!

Anyway, since I am not a fan of Vista (whatever that is — I’ve never even seen it), and I still do .Net development (maintenance only! anything new is Ruby on Rails!), and I’m not interested in dual boot with Fedora or Ubuntu, I went with Windows XP Professional. It took a while to find a manufacturer who has a a notebook with good specs and XP drivers on their website. The price and specs of this notebook combined with the fact that it supports XP Pro was unbelievable. There are a few not-so obvious things when it comes to installing XP Pro:

  • First, go into the BIOS and disable S.M.A.R.T on the hard drive (this is on the main screen, and it is an option for the hard drive)
  • Next, while in the BIOS, disable AHCI (I don’t remember which menu, but the BIOS is very limited)
  • Most importantly, you must have an XP SP2 disc. The original XP disc from 8 or 9 years ago won’t work because it clashes with the PCI express video card, and I don’t know of an easy way to tell the video card to revert to a compatibility mode. If you are stuck with an original XP Pro disc, you will have to slipstream XP SP2 into it. It is easy — probably easier than trying to tinker with the video card. Just Google “slipstream XPSP2″ and you will have a plethora of tutorials

Create Table From SQL Statement in T-SQL and MySQL

Tuesday, April 14th, 2009

I frequently make a quick copy of a table before tinkering with the data within it. Since I use both SQL Server and MySQL, I always have to look up the syntax of how to do this because I never remember.

In T-SQL, this is the syntax:

select *
into MyTableBackup_20090414
from MyTable

In MySQL, this is the syntax:

create table MyTableBackup_20090414 as
  select *
  from MyTable