<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"><channel><title>Morten Bock</title><link>http://www.mortenbock.dk/</link><description>A feed of new content on my website</description><item><author>Morten Bock</author><category>english</category><category>javascript</category><category>link</category><category>popup</category><description>                &lt;p&gt;
                    I was asked today how I would go about looping through an html page and attach an
                    onclick event for all links with a certain class. So I decided to make an example
                    and post it here.&lt;/p&gt;
                &lt;p&gt;
                    What I am going to do is mark all links that should function as a popup window with
                    the class "popup", and then I am going to intercept the click event on those links
                    by using unobtrusive javascript.&lt;!--more--&gt;&lt;/p&gt;
                &lt;p&gt;
                    Here it goes. The html markup we will use looks like this:&lt;/p&gt;
                &lt;p&gt;
                    &lt;pre&gt;&amp;lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&amp;gt;
&amp;lt;html xmlns="http://www.w3.org/1999/xhtml" &amp;gt;
&amp;lt;head&amp;gt;
&amp;lt;title&amp;gt;Untitled Page&amp;lt;/title&amp;gt;
&amp;lt;script type="text/javascript" src="addpopuplinks.js"&amp;gt;&amp;lt;/script&amp;gt;
&amp;lt;/head&amp;gt;
&amp;lt;body&amp;gt;
&amp;lt;p&amp;gt;This is a text with a few links in it.
&amp;lt;a href="http://www.google.com/"&amp;gt;This is a regular link&amp;lt;/a&amp;gt; which just goes to a new webpage.
&amp;lt;a class="popup" href="http://www.umbraco.org/"&amp;gt;This is marked with a class "popup"&amp;lt;/a&amp;gt; and will recieve a javascript onclick event when the document is loaded.&amp;lt;/p&amp;gt;
&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;&lt;/pre&gt;
                &lt;/p&gt;
                &lt;p&gt;
                    And the javascript looks like this:&lt;/p&gt;
                &lt;pre&gt;
//Add multiple functions to the window.onload event.
function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            oldonload();
            func();
        };
    }
}
//Add the onclick event to link of a certain class
function makePopupLinks() {
    //Check that browser supports the used methods
    if (!document.getElementsByTagName) { return false; }
    //Get all links in document
    var links = document.getElementsByTagName("a");
    //Loop through links
    for (i = 0; i &amp;lt; links.length; i++) {
        //Check for the right classname
        if (links[i].className == "popup") {
            //Add the onclick event to the object/element
            links[i].onclick = function() {
                window.open(this.getAttribute("href"), "popupwindow", "width=400,height=400");
                //Return false so that the link is not followed in the main window
                return false;
            }
        }
    }
}
addLoadEvent(makePopupLinks);
                &lt;/pre&gt;
                &lt;p&gt;
                    The comments in the code should be more or less self explanatory but if you have
                    any questions, feel free to post a comment :-)&lt;/p&gt;</description><guid isPermaLink="false">1312</guid><link>http://www.mortenbock.dk/blog/2008/01/07/converting-links-to-popups-using-javascript.aspx</link><pubDate>Mon, 07 Jan 2008 22:30:25 +0100</pubDate><title>Converting links to popups using javascript</title></item><item><author>Morten Bock</author><category>english</category><category>statistik</category><category>google-analytics</category><category>javascript</category><category>linktracking</category><description>
&lt;p&gt;I wanted to try and find a way to track all of the outgoing
links on my blog, without haven to modify the html of the link.
What I found was that using javascript listeners would probably be
the best solution. Actually this technique can be used for a lot of
stuff, but this is what I did:&lt;!--more--&gt;&lt;/p&gt;

&lt;pre&gt;
&amp;lt;script type="text/javascript"&amp;gt;
// Cross-browser implementation of element.addEventListener()
function addListener(element, type, expression, bubbling) {
    bubbling = bubbling || false;

    if (window.addEventListener) { // Standard
        element.addEventListener(type, expression, bubbling);
        return true;
    } else if (window.attachEvent) { // IE
        element.attachEvent('on' + type, expression);
        return true;
    } else return false;
}

//This is what i want to do whenever someone clicks on the page
function itHappened(evt) {

    //Get the clicket element
    var tg = (window.event) ? evt.srcElement : evt.target;
    //If it is an A element
    if (tg.nodeName == 'A') {
        //And it is not an internal link
        if (tg.href.indexOf(location.host) == -1) {
            //Replace all odd characters, so that it works with Analytics Niavgation analysis
            var url = tg.href.replace(/[^a-z|A-Z]/g, "_");

            var txt = tg.innerHTML.replace(/[^a-z|A-Z]/g, "_");
            var str = '/outgoinglink/-' + txt + '-' + url;
            try {
                //Track it
                urchinTracker(str);
            }
            catch (err) {
                //alert('error: ' + err);
            }
        }
    }
}
//Add the click listener to the document
addListener(document, 'click', itHappened);&amp;lt;/script&amp;gt;
&lt;/pre&gt;

&lt;p&gt;I hope you can use it. Any comments or advice is very welcome
:-)&lt;/p&gt;
</description><guid isPermaLink="false">1199</guid><link>http://www.mortenbock.dk/blog/2007/04/15/google-analytics-outgoing-links.aspx</link><pubDate>Sun, 15 Apr 2007 23:00:10 +0100</pubDate><title>Tracking all outgoing links with Google Analytics</title></item></channel></rss>
