SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    PHP Developer W1LL's Avatar
    Join Date
    Apr 2001
    Location
    Leicester, UK
    Posts
    459
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Alternative to target="_blank"

    I'm just updating all my markup on my site to meet the W3C's XHTML 1.0 Strict standards, and one thing that it says is that you can't use target="_blank".

    I'm just wondering if anyone knows a simple JavaScript alternative for this?

    - Will

  2. #2
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    This is the way I go about opening new windows in Strict mode. If a user has JS enabled it will spawn a new window when clicked. Otherwise, the link will open in the same window.
    HTML Code:
    <a href="mypage.html" onclick="window.open(this.href); return false;">My Page</a>

  3. #3
    PHP Developer W1LL's Avatar
    Join Date
    Apr 2001
    Location
    Leicester, UK
    Posts
    459
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nice one, thanks a lot!

  4. #4
    SitePoint Zealot johno's Avatar
    Join Date
    Sep 2003
    Location
    Bratislava, Slovakia
    Posts
    184
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  5. #5
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by johno
    Yeah, I like your function if you have a bunch of links that spawn new windows on your site. For one-off purposes though, I like mine .

  6. #6
    SitePoint Zealot johno's Avatar
    Join Date
    Sep 2003
    Location
    Bratislava, Slovakia
    Posts
    184
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by vgarcia
    Yeah, I like your function if you have a bunch of links that spawn new windows on your site. For one-off purposes though, I like mine .
    This is not about likes or dislikes. Your implementation may not work correctly if popups are blocked by user. It just says open window and then return false. Is window realy opened? Your script doesn't care.

  7. #7
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by johno
    This is not about likes or dislikes. Your implementation may not work correctly if popups are blocked by user. It just says open window and then return false. Is window realy opened? Your script doesn't care.
    Would that not be the fault of the popup blocker then? Only popups that aren't triggered by a user's actions should be blocked, not ALL popups.

  8. #8
    SitePoint Zealot johno's Avatar
    Join Date
    Sep 2003
    Location
    Bratislava, Slovakia
    Posts
    184
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If I move a mouse and a popup runs out have I triggered it? This is only about clicking?

    AIf I block all new window openings? I don't get the site I've wanted. (return false

  9. #9
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by johno
    If I move a mouse and a popup runs out have I triggered it? This is only about clicking?
    You're right, I should have phrased that better. I meant requested popups should be allowed, like that found in Mozilla's popup blocker. Mozilla's blocker stops onload and onunload-driven windows from opening, but allowed clicked or keyboard-requested new windows to show.

  10. #10
    Sultan of Ping jofa's Avatar
    Join Date
    Mar 2002
    Location
    SvÝ■jˇ­
    Posts
    4,080
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by vgarcia
    This is the way I go about opening new windows in Strict mode. ...
    Or, if you think it's boring writing all those onclick attributes (), you can include a function like this:
    Code:
        // Open external links in new window
        // Modified version of Kevin Yank's script
        // http://www.sitepoint.com/print/1041
        function externalLinks() 
        { 
          if(!document.getElementsByTagName) 
          {
            return;
          } 
          var anchors = document.getElementsByTagName("a"); 
          for(var i = 0; i < anchors.length; i++) 
          { 
            var anchor = anchors[i]; 
            if((href = anchor.getAttribute("href")) &&
              (rel = anchor.getAttribute("rel")))
            {
              var args = rel.split(",");
              if(args[0] == "external") 
              {
                if(args.length < 2)
                {
                  anchor.target = "_blank"; 
                }
                else
                {
                  var name = anchor.title.replace(" ", "_");
                  var width = args[1] ? args[1] : 500;
                  var height = args[2] ? args[2] : 400;
                  eval("anchor.onclick = function(){" +
                    "var win = window.open('" + href + "'," + 
                      "'" + name + "', " +
                      "'width=" + width + ", " +
                      "height=" + height + ", " +
                      "resizable=yes');" + 
                    "win.focus();" +
                    "return false;}");
                }
                anchor.title += " (Will open in a new window)";
              }
            }
          } 
        }
        window.onload = externalLinks;
    And then write links like:
    Code:
          <a href="http://example.com"
            rel="external,800,600" title="Go to example.com">
            Example.com
          </a>
    Note the extra information added to the link's title


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •