How do I make a link open in a new window? (XHTML compliant!)

Discussion in 'HTML & Website Design' started by irideflatland, Aug 29, 2006.

  1. #1
    I decided to use the validator tool thing for the first time, and 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?
     
    irideflatland, Aug 29, 2006 IP
  2. frankcow

    frankcow Well-Known Member

    Messages:
    4,859
    Likes Received:
    265
    Best Answers:
    0
    Trophy Points:
    180
    #2
    <a href="document.html" rel="external">external link</a>
     
    frankcow, Aug 29, 2006 IP
  3. frankcow

    frankcow Well-Known Member

    Messages:
    4,859
    Likes Received:
    265
    Best Answers:
    0
    Trophy Points:
    180
    #3
    if that works for ya, a green point is always appreciated!
     
    frankcow, Aug 29, 2006 IP
    irideflatland likes this.
  4. irideflatland

    irideflatland Banned

    Messages:
    515
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #4
    It didn't work, but I'll green you for effort ;)
     
    irideflatland, Aug 29, 2006 IP
    AdamSee likes this.
  5. AdamSee

    AdamSee Well-Known Member

    Messages:
    422
    Likes Received:
    28
    Best Answers:
    0
    Trophy Points:
    135
    #5
    Unfortunately it isn't valid anymore. There's a bit of an argument about it, but that's another story.

    For 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.
    
    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();
        }
      }
    }
    
    Code (markup):
    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.
     
    AdamSee, Aug 29, 2006 IP
    frankcow likes this.
  6. irideflatland

    irideflatland Banned

    Messages:
    515
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #6
    ^ Thanks :)
    I'd green you, but it says I have to spread it around first.
     
    irideflatland, Aug 29, 2006 IP
  7. frankcow

    frankcow Well-Known Member

    Messages:
    4,859
    Likes Received:
    265
    Best Answers:
    0
    Trophy Points:
    180
    #7
    you sir deserve some green!!!
     
    frankcow, Aug 29, 2006 IP