Posts filed under 'JavaScript'

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>

Prototype Cheat Sheet

Marc-Antoine Ross March 17th, 2006 Add comment

Here is a link to a very good cheat sheet on Prototype by Jonathan Snook: Snook.ca.

An indexable popup… it’s possible!

Mario Laflamme March 10th, 2006 1 comment

The use of popups is very wide-spread, but most of the time they are not indexable. Nevertheless, there is a very simple way to conceive popups which can be crawled by the robots of search engines.

Demonstration

Insert the url of the popup in the href tag and insert the function which opens the popup in the onclick tag followed by the return false; command as in the following example:

<a href=”example.html” onclick=”openPopup(this.href);return false;”>popup example</a>
Explanations

This way allows the robots to follow the link in the href tag while opening the link in a popup with the openPopup() function which gets back the content of the href tag.

XHTML: Open a link in a new window

Mario Laflamme March 9th, 2006 1 comment

Sure, you can to use the tag target=”_blank” to open a link in a new window with XHTML, but this tag is not valid. A simple and effective alternative is to insert the following JavaScript function in your pages and to add the tag rel=”external” to the appropriate links.

<script type=”text/javascript” language=”JavaScript”>
    function externalLinks() {
        if (!document.getElementsByTagName) {
            return;
        }
        var anchors = document.getElementsByTagName(”a”);
        for (var i=0; i<anchors.length; i++) {
            var anchor = anchors[i];
            if (anchor.getAttribute(”href”) &&
                anchor.getAttribute(”rel”) == “external”) {
                anchor.target = “_blank”;
            }
        }
    }
    window.onload = externalLinks;
</script>

Schedule

July 2008
M T W T F S S
« May    
 123456
78910111213
14151617181920
21222324252627
28293031