Hi ColinABQ,
As a quick solution to this, without having to modify code, you could run this Javascript in your theme:
$(document).ready(function() {
$('a').attr('target', '_blank');
});
The above script, written in jQuery, will add or modify the “target” attribute of all ‘a’ tags to being ‘_blank’. This could affect the speed and performance of your website, although it is a non-destructive and solid solution to this issue. 🙂
Sorry Colin, but as a theme developer I disagree with you. Links should not be opened in new Window.
Here are a couple of articles explaining why:
Old article on Sitepoint, but still true and valid
And here’s a good one from Smashing Magazine.
If you are wanting to keep users on your site, then you need to give them what they want and provide great content. Opening links in a new window won’t keep them there.
Thanks for the input, I appreciate it.
I also appreciate, and accept, some of the many reasons for it being a bad idea, a lot of the time. I just think that there enough instances of when it is a good idea, within the typical WordPress site, that it should become standard practice in most cases. Again, I think the wordpress.org and theme/plugin developer links should be considered augmenting and supporting.
I also acknowledge that it can be tough to figure when to do it, and when not to. The keys are, what content to consider augmenting, and somehow warning the user that new tabs or Windows will be used in those cases.
My rant wasn’t about keeping visitors at my site – my content needs to do that. My rant was about users, and me, having those links clobber whatever I was viewing or working on, and then having a heck of a time getting back there when I’m done with the augmenting or supporting information that was linked to. The wordpress.org and developer links are not, “please come read my stuff, it is much cooler than this” links, they are, “if you need help or want to know more about this, here you go.” I hope we can all understand that difference, and why at least some such links should launch in new Windows or tabs. Think of them the same way you think of “help” or “change your settings” pop-ups that some sites use.
Regardless, I’ve softened : ) We can all be right, to a degree.
Again, thanks.
similar to what mattyza proposed but the below will open all links starting with http (typically your external ones!) in new window
$(document).ready(function(){
$("a[@href^='http']").attr('target','_blank');
});
I guess one main reason for the demise of “target” is that its not valid for XHTML 1.0 strict doctypes. One of the main aims of XHTML and valid code is to make your content “platform independant”. The concept of a new window often doesnt apply to mobile web browsers.
mattyza,
Which file should I put this in if this is what I want to achieve?
vooboo13,
both mattyza and my code would be placed in a javascript file (or within script tags) if you have the jQuery javascript library installed on your site also.
the below is an improvement from the above as it doesnt affect absolute cross links to your own site.
$(document).ready(function(){
$("a[href*='http://']:not([href*='"+location.hostname+"'])").attr("target","_blank");
});