Go Back   CP Web Hosting - reseller hosting > Browser Scripting, HTML, XML and Multimedia > HTML/XHTML
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-21-2007, 11:16 AM
Senior Member
 
Join Date: May 2007
Posts: 276
Default target="_blank" error

when i use the validator tool for my site, I found a bunch of errors. One of them was using target="_blank" to open a link in a new window. Is there another way to do this?
Reply With Quote
  #2 (permalink)  
Old 06-21-2007, 11:20 AM
Senior Member
 
Join Date: May 2007
Posts: 334
Default

There is an unobstrusive way of doing it, use JavaScript.


Create a file called externallinks.js and add this code:

All it does is find every element in the page with the 'a' attribute and if it has the rel="external" attribute, it will add the target="_blank" behaviour.

Code:
addLoadEvent(externalLinks);

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";
    }
}
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}
Link to it within the <head> part of your document:

<script src="externallinks.js" type="text/javascript"></script>


Each link you want to popup a window just add the rel="external" to it. Techically, it still won't make your page valid, because you're adding the attribute, target="_blank", through JavaScript. However, validators won't pick this up and many people use a method similar to this. So your page will be valid as such.
Reply With Quote
  #3 (permalink)  
Old 06-21-2007, 11:21 AM
Senior Member
 
Join Date: May 2007
Posts: 329
Default

Try this <a href="document.html" rel="external">external link</a>
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 03:00 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.1.0