Archive for February 3rd, 2007

XHTML: Open a link in a new window - V2

Marc-Antoine Ross February 3rd, 2007 Add comment

I used my friend Mario’s great post on how-to open a link in a new window to create a script that will open any external link in a new window without the need to add a CSS class to it. This is very useful when you want to change the behavior on a blog that already has a lot of posts.

<script type=”text/javascript” language=”JavaScript”>
    function externalLinks() {
        if (!document.getElementsByTagName) {
            return;
        }
        var anchors = document.getElementsByTagName(”a”);
        var sDom    = document.domain;
        for (var i=0; i<anchors.length; i++) {
            var anchor = anchors[i];
            if (anchor.getAttribute(”href”)) {
                iPosition = anchor.href.indexOf(sDom);
                if (iPosition == -1 || 
                    iPosition > sDom.length) {
                    anchor.target = ”_blank”;
                }
            }
        }
    }
    window.onload = externalLinks;
</script>

Schedule

February 2007
M T W T F S S
« Nov   Apr »
 1234
567891011
12131415161718
19202122232425
262728