UJS Popups: New windows with unobtrusive JavaScript

JavaScript, code, UJS

For popup / new windows this is the JavaScript I use.
It is taken from Jeremy Keith's book, Dom Scripting. It's the best...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
function preparePopups() {

        if (!document.getElementsByTagName) return false;
        if (!document.getElementById) return false;
        var links = document.getElementsByTagName("a");
        for ( var i=0; i < links.length; i++) {
                //Checkif popup is in the styles
                if(links[i].className.indexOf("popup") != -1 || links[i].title.indexOf("popup") != -1 ) { 
                        title = links[i].getAttribute("title")                        
                        links[i].onclick = function() {        
                             return popup(this,title,this.className, this.title);
                        }
                        links[i].onkeypress = links[i].onclick;
                }
        }
}