XHTML Strict - Open Link in New Window

Associate
Joined
28 Nov 2005
Posts
431
Location
Scotland
How do you open a link in a new window in XHTML Strict?

Theres the classic target="_blank" but this does not validate as valid strict HTML.

How do you do this so that it validates, Is javascript required?
 
I have done it with js as follows:

Code:
<a href="###" rel="external">link</a>

this then calls the js below:

Code:
<script type="text/javascript">
function externalLinks() { 
 if (!document.getElementsByTagName) return; 
 var anchors = document.getElementsByTagName("a"); 
 for (var i=0; i<anchors.length; i++) { 
   var anchor = anchors[i]; 
   if (anchor.getAttribute("href") && 
       anchor.getAttribute("rel") == "external") 
     anchor.target = "_blank"; 
 } 
} 
window.onload = externalLinks;
</script>
 
It's much neater with jQuery :).
Code:
jQuery(function($) {
  $('a[rel=external]').attr('target', '_blank');
}
 
I never understood why this isn't part of Strict. It's fairly common to want to open another window for legitimate reasons - eg. to view a PDF.

I've had this before and I made the decision to downgrade to Transitional, although that little bit of jQ is quite an elegant way of doing it.
 
I never understood why this isn't part of Strict. It's fairly common to want to open another window for legitimate reasons - eg. to view a PDF.

I've had this before and I made the decision to downgrade to Transitional, although that little bit of jQ is quite an elegant way of doing it.

Well the usual reason is that it takes choice away from the user; it's the user's prerogative to open links in new windows/tabs, rather than the website's. What if I want to open the PDF in the same window? There'd be no way of doing so if this behaviour is enforced.
 
Last edited:
Well the usual reason is that it takes choice away from the user; it's the user's prerogative to open links in new windows/tabs, rather than the website's. What if I want to open the PDF in the same window? There'd be no way of doing so if this behaviour is enforced.

+1

Ultimately sad but true considering sometimes you don't realise what the target of the link is before it's too late. I quite often find myself flicking through tabs looking for the webpage I was on only to find that I have to go back from a PDF or other file that I opened by accident, expecting a link to another page on that site.

There's a JQuery plugin that quite nicely adds icons next to your links automatically based on the target. (E.g. PDF icon if linking to PDF and wee "external link" icon for links that navigate away from your site) Means the user will be more informed before clicking on a link. Shame it's not more common place and that I can't find it right at this second! :)

Roy
 
As a side note, it's interesting to see that target="_blank" is valid in HTML5.
 
Back
Top Bottom