On 17/10/2007, at 1:05 PM, Chris Knowles wrote:

heres a generic javascript function I wrote to open links in a new
window based on class name. It's only a partial solution to the pdf
issue but maybe someone will find it useful anyway.

just call it on dom load or window load with the class name you want to use:
setNewWindowLinks('new-win');

It'll hijack any 'a' tags with the class name you use and make them open
in a popup. If no javascript enabled then it'll just go to that link.
The 'a' tag can have multiple class names as well and it'll still work.

function setNewWindowLinks(className)
{
    var tags = document.getElementsByTagName('a');
    var re = new RegExp(className);
    if (tags.length > 0) {
        for (var i = 0; i < tags.length; i++) {
            if (tags[i].className.search(re) != -1) {
                tags[i].onclick = function()
                {
                    window.open(this.href, '_blank');
                    return false;
                }
            }
        }
    }
}

Just a note:
Your function doesn't currently use the RegExp function for anything useful (you might as well use indexOf). RegExp is the right way to do it, though, so you can enforce word boundaries to match complete classNames only (if I want all a.pop to be new window links, I wouldn't want a.popcorn to turn into a popup window).

See http://snook.ca/archives/javascript/your_favourite_1/ for more info (specifically the update) on how to enforce word boundaries but allow for multiple classnames.

Just my thoughts,

Kit

*******************************************************************
List Guidelines: http://webstandardsgroup.org/mail/guidelines.cfm
Unsubscribe: http://webstandardsgroup.org/join/unsubscribe.cfm
Help: [EMAIL PROTECTED]
*******************************************************************

Attachment: smime.p7s
Description: S/MIME cryptographic signature

Reply via email to