Friday, August 14, 2009

Keyword-Links - Open external links in new window

Few days back, I was working on a clients website which was built on Joomla. Client asked me to install Keyword-Links component on her Joomla website. I never had used this component before. As per the description from Joomla Extensions Directory, Keyword-Links is a native Joomla 1.5 extension that enables Joomla websites to dynamically establish links for keywords or keyword-phrases in Joomla articles. With this extension, the administrator can manage all internal and external links in articles in a central location at the backend. Keyword-Links supports unlimited keywords for link-management and advertising.

No doubt about usefulness of this component but still I found a major draw-back in it. You can not configure this component to open all external links in new window or tab. Therefore, this component can reduce the stay time of visitor on your website.

To overcome this issue, I had to find a way to open all the external links to a new window. And after spending couple of hours, I realized that instead of changing component's core files, I should dynamically add "target" attribute to all the external links. Therefore, I included following Javascript in the index.php of template.


<script type="text/javascript">
function getElementByClass(theClass) {
var allHTMLTags=document.getElementsByTagName("a");
for (i=0; i<allHTMLTags.length; i++)
{
if (allHTMLTags[i].className==theClass)
{
allHTMLTags[i].target='_blank';
}
}
}
getElementByClass("keyword_extern");
</script>


Make sure that you paste this code in the bottom of template's index.php.

The above code is very easy to understand and self explanatory.

No comments:

Post a Comment