Easy XHTML open link in new window
As you probably already know, in XHTML, the target="_blank" attribute for a link, which causes it to open in it’s own window, is no longer valid. This demands for there to be a equally easy way to make clicked links open in new windows without the target link attribute and JavaScript is the answer.
With JavaScript we can find all the links with a certain class and set their targets so that they open in a new window. Here is the JavaScript, it takes advantage of some of the features of the Prototype JS framework.
$$('a.blank').each(
function (link) {
link.observe('click', function (ev) {
window.open(link.href, '_blank');
ev.stop();
});
}
);
Here we are saying, find all the links on the page with a class of blank, and set their target to blank.
Paste this into the footer of your site and it will give you XHTML compliant links that open in a new window.
1 comment
This is great! Thank you.
— Philippe Sat, 3 Oct 2009