Tuesday 24 November 2009

Handy MooTools script for phpBB3 inside a wrapper

For reasons that are too long to go into, a change between phpBB2 and phpBB3 was to make all links not include a target attribute to make the output meet relevant requirements.

On one site I manage (http://www.connectage.com) we have a forum inside a Joomla! wrapper. I'm sure you can see where this is going.

So to stop this happening, I wrote a MooTools script. This script ensures that links to our site open in the parent window (i.e. not the iframe) and other links open in a new window / tab.


<script src="../media/system/js/mootools.js" type="text/javascript"></script>
<script type="text/javascript">
window.addEvent('load', function() {
// send links to a new window, unless it is on our site
var postLinks = $$("div#pagecontent div.postbody a.postlink");

postLinks.each(function(link) {
var sendTo = '_blank';
if(link.getProperty('href').contains(window.location.hostname)) {
sendTo = '_parent';
}
link.setProperties({
target: sendTo,
rel: 'nofollow'
});
});
});
</script>


Simple as that.

There are two scripts there. 1 brings in the default Joomla! MooTools script, which is needed so the rest works.

The second bit, creates an array of links within posts only, checks the domain they're going to and sends them to the parent window (target='_parent') or a new window (target='_blank').

And because it's Javascript, I believe it's still standards compliant. It will also not stop your site working if someone has Javascript turned off in their browser.

This may not work on your template, but it should be easily adaptable if you need it for your own site.

No comments:

Post a Comment