I recently had a client that thought it was too much work to always have to select the “_blank” target open when adding new links to WordPress. I Googled around and saw a lot of simple solutions but they all involved complex setup or customizing core wordpress code – which I hate because it breaks with every upgrade.
What I need turned out to be simple. A few lines of JavaScript and the jQuery library (which was available in the theme already) and before Masterchef had finished everything was working exactly as the client wanted and I was confident that the theme and the wordpress install would be stable and upgradeable.
Here is the function as I wrote it (in a theme that already included jQuery and had the noConflict setting)
<script language="javascript">
jQuery(document).ready(function() {
jQuery('ul.blogroll li a').each(function(i)
{
jQuery(this).attr('target', '_blank');
}
)
});
</script>
If you had to implement from scratch you’d need to download (or include it straight form Google) and include jQuery and your code would probably need to run like
<script type="text/javascript" src="jquery.js"></script>
<script language="javascript">
$(document).ready(function() {
$('ul.blogroll li a').each(function(i)
{
$(this).attr('target', '_blank');
}
)
});
</script>
|
Share the Love
|
Get Free Updates
|
Related posts:
- Open links in a new window and still validate
- WordPress Development
- Make Google Friend Connect Work with Thesis
- What is the best blog platform for a business ?
- Integrating Big Commerce and WordPress
Tags: Javascript, Wordpress
