When it comes to website management links are huge. Most people realize that if they have a link off their site that it is a possibility that they will loose person. Now, if the goal for your website it to keep customers where they can buy from you, it makes sense to never send a customer away.
However, the web is an amazing place with a huge amount of information. Your site can’t have all the info. Also, linking to quality content will also improve your SEO. So, you might just end up with more people visiting your site if you share the wealth around a little. But how can you have your cake and eat it too? Well, you could open a new browser window when you were sending someone to another site. That would mean that when they closed that windows, your site would still be there. Just so they don’t get lost.
Today, I am going to produce some code that will automatically open a new window for any external link and also show you how to dress up these links so that people will know that they are going to take them off your site. Hopefully, it will be a useful tutorial in some of the great features built right into jQuery.
jQuery Selectors
jQuery has some amazing capabilities when it comes to its built in selectors for html elements. In this case, it makes good sense to use a selector to get all the links on the page and then use the jQuery each() function to iterate through each found link and test it to see if it links to an external site.
We’ll test if the link’s hostname matches the pages hostname. If they don’t we’ll know that the link is to another site and then we can do some additional work on it. The code looks like this:
Anchor Tag Selector and External Link Test
$("a[href*='http://']:not([href*='"+location.hostname.replace
("www.","")+"'])").each(function() {
//do some 'stuff' to the external link
});
Alter The External Link
I want to add a new class to each external link (I’ll cleverly call this class ‘externalLink’). We’ll use the jQuery .addClass() function. We’ll use the .click() function to modify the existing onclick event for the link. Note: we could have added a target=”_blank” attribute, but that would not be xhtml compliant. These can be chained onto one line for efficiency.
Add Class and window.open Event
jQuery(this).click(function(event) {
event.preventDefault();
event.stopPropagation();
window.open(this.href, '_blank');
}).addClass('externalLink');
At this point the code is also complete. We want this code to fire when the page loads. So, we will wrap the code in the jQuery .ready() event handler. There is a shorthand that is great, we’ll use it here.
Function to Execute After the DOM is Ready.
$(function() {
// Handler for .ready() called.
});
Lastly, I wanted to style the external links differently from all the other links. We have already added the new class, ‘externalLink’, to the link element. I wanted to add an icon similar to how Wikipedia does. So, we’ll want to add a couple lines of css to the styles.
External Link CSS
.externalLink {
background: url("/images/externalLink.png") no-repeat scroll right center transparent;
padding-right: 13px;
}
Putting It All Together
When we put it all together it is a simple, elegant and extremely useful solution. Here is the final code:
jQuery Open External Links in New Window
$(function() {
$("a[href*='http://']:not([href*='"+location.hostname.replace
("www.","")+"'])").each(function() {
$(this).click(function(event) {
event.preventDefault();
event.stopPropagation();
window.open(this.href, '_blank');
}).addClass('externalLink');
});
});
.externalLink {
background: url("/images/externalLink.png") no-repeat scroll right center transparent;
padding-right: 13px;
}
comments powered by Disqus