Using external for rel attribute on a link, you can specify that the link is outside of the current site. This attribute is commonly used for SEO and styling options. And to open a link in a new window when user clicks it you can use target attribute. What if you wanted all external links to open in a new window? jQuery is the answer.
To easily add target blank attribute to all external links you can use attribute selectors that come with jQuery:
jQuery(function() { jQuery('a[rel~=external]').attr('target', '_blank'); });
This simple line of code adds target="_blank" attribute to all external links on your site. However, this should be done when the DOM is loaded. So we need to wrap this code with the document ready function like this:
<script type="text/javascript">
/* <![CDATA[ */
jQuery(document).ready(function() {
//External links
jQuery(function() { jQuery('a[rel~=external]').attr('target', '_blank'); });
});
/* ]]> */
</script>
If you want this trick for your wordpress site, put this code in footer.php in your theme just before </body> tag. This way all your rel="external" links will open in a new window.
Hope you like this simple trick.
Read more tips down here:
- Displaying content based on current language using qtranslate
- How to use default WordPress RSS widget in your php templates
- Dropdown Menu Widget update 1.9
- Countdown Widget Update 2.5
- Add browser classes to body using filters in WordPress
- Facebook Meta Tags plugin update
- Image Banner Widget Update
- WP Trick : Display most viewed posts first on search pages
- Book Review : WordPress 3 for Business Bloggers
- Displaying random posts using shortcode
- Dropdown Menu Widget v1.8 released
- Facebook Meta Tags plugin update
- Customizing WordPress login page without plugin
- New WordPress Plugin : WP-Lock
- z-Index Fix plugin for WordPress
- How to Exclude Pages from Search Results in WordPress
- Randomize posts on your archive pages
- Shailan Theme Framework 3.2 Released
- SEO Tip : How to add post category to document title
- How to enable post thumbnails for pages
- Twitterify 1.2beta
- How to get page id using page slugs in wordpress
- Dropdown Menu Widget Template Tag Usage Explained
- How to make auto-embedded videos obey z-index
- Video tutorial : How to create a custom dropdown menu on your site
- Dropdown Menu Widget 1.7.1 hotfix
- Create a Dropdown Menu Using CSS3 Transitions
- Adsense Widget 1.6 Updates
- 8 Useful Tips To Stay Focused at Home
- Feedburner’s new interface in beta
- How to add Google +1 button to your WordPress theme
- How to add a custom background to your theme
- How to exclude categories and tags from your widgets
- Top 5 WordPress permalink structure for SEO
- WordPress theme developers cheatsheet
- How to display a “Back to parent” link on your blog pages
- New widget : Include Template Widget
- How to list files in a directory using PHP
- Twitterify update 1.1
- How to display latest tweet on your site
- Dropdown Menu Widget update 1.6.3
- Just created a feedback form for my plugins
- How to enable excerpts for pages
- Adding countdown to your theme using php
- How to add a countdown to your post using shortcode
- WordPress 3.1 is here. Yay!
- Subpages Extended Update 1.3.4
- A simple trick for testing current page using page slugs
- Twitterify plugin now available from wordpress.org
- How to redirect to single post page if there is one post in category/tag
- Shailan Theme Framework – First Release
- New plugin : Twitterify
- How to enable comments for all posts in wordpress
- How to add categories dropdown to the end of Dropdown Menu Widget
- How to use shortcodes in your theme files
- Superfish Dropdown Menu Update v1.1
- How to display site logo on facebook share if no post thumbnail exists
- Adding javascript to your theme using wp_enqueue_script
- How to change default embed size using filters
- Superfish dropdown menu update
- Twentyeleven theme minor fixes
- How to display Nextgen Gallery using php
- Building Campaign links for Google Analytics
- TwentyEleven – widgetized twentyten theme
- Introducing dropdown menu effects
- Subpages Extended : Sorting your pages list with shortcode
- How to fix common RSS feed errors in wordpress
- Opening external links in a new window using jQuery
- Enabling shortcodes in widgets
- How to get your site back when it stuck in maintenance mode
- WordPress security update
- Speed up your page load times with lazyload
- How to disable wordpress admin bar
- Subpages Extended update
- Facebook Meta Tags update
- Customizing countdown widget using CSS
- Beautify your site in 3 simple steps using Google fonts
- How to change HTML editor font in WordPress
- How to find ID of a page
- Adding jquery tooltips to your wordpress theme
- How to change dropdown menu depth and order using filters
- Changing adsense colors to fit your theme
- Tutorial : Combining multiple shortcodes in wordpress
- How to install wordpress using wordpress downloader script
- How to hide members only content in your posts
- 25+ Free Online Web Design Tools You Should Bookmark
- WordPress Countdown Widget 2.0
- New wordpress plugin : Adsense widget
- SEO Tip: highlight nofollow links for authors in wordpress
- Advanced adsense targeting for wordpress Pt.2
- See what people are searching on your blog
- Advanced adsense targeting for your post content Pt.1
- Adding a simple admin menu to dropdown menu widget
- Pick your own dropdown menu colors
- Image Banner Widget Update 1.1
- Add blog updates box to your site using rss reader script
- WordPress pushes the limits with PHP & Mysql
- Custom Dropdown Menu Using WordPress 3 Nav Menus
- Dropdown Menu Widget Update 1.5.2
- WordPress 3.0 is here!
- 5 ways to hide your private content in wordpress
- Dropdown Menu Widget Update: 1.4.0
- image banner widget update, it now supports category selection
- How to change post thumbnail crop position in wordpress
- Adding “nofollow” option to WordPress add link page
- how to widgetize your theme in 3 simple steps!
- Displaying correct post thumbnail on facebook share
- Tutorial: Create your very own dropdown menu stylesheet
- Sh*t happens..
- Top 10 WordPress Plugins You Should Have
- Get your countdown widget now!
- New plugin! Subpages
- the_widget : the way to use a widget whereever you like
- New dropdown theme!
- WordPress dropdown menu widget
- How to get/save favicon of a site
- WP Developer? Do yourself a favor now..
- How to add text to every post in your feed
- WordPress 2.8 Released!
- How to convert database charset to UTF8
- How to export registry key
- Using xcopy to update/backup your working folders
