Sx
All external links from the site should open in a new window. Do not want to write this on a link-by-link basis. Should be light and unobtrusive. Using MooTools.
Dx
We can easily know what domain the script lives on. Other domains are easily distinguished. Links come and go and are loaded in, sometimes, by other links. We will use one of my favorite patterns, which I am calling the Passive Click Pattern (got a better or more widely-used name?) to listen to all clicks anywhere on the document.body. In this case, if the click is on a link and the link is external, then we will set the target of the link to ‘_blank’. Paramount to using the PCP (OMG, I did not realize that was the acronym) is the pattern of not stopping clicks in other patterns but just using preventDefault. That way, the click can flow through the DOM and be heard by multiple listeners.
This solution requires only the most core of MooTools and is built against version 1.2.3.
Tx
window.addEvent('domready', function(){
var currentDomain = window.location.host;
$(document.body).addEvent('click', function(evt) {
$trgt = $(evt.target);
if ($trgt.get('tag') !== 'a') {
$trgt = $trgt.getParent();
}
// if the target is a link AND the link is absolute AND the link goes to a different domain,
// then set the target to _blank to open in new page
if ($trgt &&
$trgt.get('tag') === 'a' &&
$trgt.get('href').test('http') &&
!$trgt.get('href').test(currentDomain)) {
$trgt.set('target', '_blank');
}
});
});
EDIT:
A more fitting name for this pattern is “Passive EVENT Pattern” since it can be applied to more than just clicks. Form submissions, for instance, or key entry.
Plus, then you can say your application is PEPpy. #amirightfolks

