Highlight External Links & Make Them Open In New Windows
You can highlight external links on your blog or website fairly easily. All you need is a small jQuery script and an even smaller CSS code snippet. And you don't even have to go back and edit your existing posts/pages for it to work.
You can also force those links to open in new windows or tabs if you want to.
Here's how:
1. Tag external links
This simple jQuery script will scan your page for external links (links pointing to different domains/subdomains) and assign them with "external" class name. This script excludes linked images because you don't want them to be highlighted. (Images for Blogger blogs are considered external since they are hosted on blogspot subdomains, other than your blog's domain/subdomain).
- If you haven't already added a jQuery library call to your template before, copy the code below and paste it above the
</head>
tag in your template.<script src='http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js' type='text/javascript'/>
- Then add this script before the
</body>
tag.<script> //<![CDATA[ (function($) { $('a:not(:has(img))').filter(function() { return this.hostname && this.hostname !== location.hostname; }).addClass("external"); })(jQuery); //]]> </script>
2. Highlight external links
Now that you've added "external" class to all external links, you can easily target (to highlight) them in CSS using "a.external" selector.
On Blogger, go to Template > Customize > Advanced > Add CSS, add the following CSS code and press the Enter key on your keyboard.
- For a simple highlight, you can just change the text or background color, like in this example:
a.external {color:red; background:yellow;}
-
Or if you want to use a symbol/icon next to the link, like on Wikipedia and Twitter Developers Site, add the following CSS snippet:
a.external {background:url("https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi6SXMgZB9gEC1Ri-Jy2ynh0JsmeX-csIa6MitojiubMDkz2JdQy23jkevufJFPYvPMGogSskbRAwQdKTxcjxp5Px9Px1rzUHxvVnEiuBLTtaPjh4OmGiWUp1bG3NdqIHCt5ZCgAzGE5Nj5/s320/Icon_External_Link+12.png") no-repeat right center; padding-right:13px; display:inline-block !important; }
To use a different icon, simply replace the background URL with that of your icon. You can download free external link icons from Shapes4free.
3. Open external links in new windows
You can make the external links open in new tabs or windows, simply by adding target="_blank"
attribute to the anchor tags. This can be achieved by altering the original script slightly, like this:
<script> //<![CDATA[ (function($) { $('a:not(:has(img))').filter(function() { return this.hostname && this.hostname !== location.hostname; }).addClass("external").attr('target', '_blank'); })(jQuery); //]]> </script>
Super easy, right?
Enjoy!
21 comments to "Highlight External Links & Make Them Open In New Windows"
Thanks for sharing this tutorial and tips, very useful.
thank you we love it, it's so useful :)
@Arham Sukardi
You're welcome :)
Love the code for the icon. Thanks a lot man.
Really super easy. Just copy and paste the code.
But, since you just mentioned about the external link, i need to gali-gali your blog to find out more about it.
thanks GL for sharing it
Hi, Can I implement this technique on my Wordpress site?? If not, please write about it.
Very helpful! Thanks :)
http://collegemarketing101.blogspot.com/
very helpful! thanks for this post :)
http://collegemarketing101.blogspot.com/
Thanks really needed that http://godwithmeblogg.blogspot.com
Business Proposal Template Software by eIntelli
Any specific advantage of highlighting external link. btw, the implementation is quite easy!
How to make it just show on my article posts ?
How to make it just show on my article posts ?
How to make it just show on my article posts ?
How to make it just show on my article posts ?
Wow that's what i was looking for..
i was getting tired writing target="_blank" code manually but this script will help me. Thanks
what a great tips thanks for sharing this beautiful tips and tricks. this is a very informative blog.
SEO
How to make this icon appear only on article ?
it's also highlighting blog comments links.
Amazing tips. Is there any way to build a little script that makes every external link as a nofollow?
Taking control of a visitor's browser is not good practice. It's best to let the user decide where to open the link.
Mind blowing tips. Thanks for sharing your great experience with us.
Post a Comment
We love to hear from you! Leave us a comment.
To ensure proper display, HTML/XML/Javascript need to be escaped first using this escape tool.