<?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></channel></rss>
