1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Any way to put target: _blank in the CSS

Discussion in 'CSS' started by Lilyz, Mar 7, 2007.

  1. #1
    Is there a way to put target: _blank in the CSS?
    I put it as bellow and didn't work.
    ul{
    target: _balank;
    }
    Thanks
     
    Lilyz, Mar 7, 2007 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,500
    Likes Received:
    4,460
    Best Answers:
    123
    Trophy Points:
    665
    #2
    No, CSS is for formatting. HTML is for the actual commands and content.
     
    sarahk, Mar 7, 2007 IP
    EGS likes this.
  3. kk5st

    kk5st Prominent Member

    Messages:
    3,497
    Likes Received:
    376
    Best Answers:
    29
    Trophy Points:
    335
    #3
    Besides that, the target attribute has not been a part of html since 1999, html4. It is not valid in any strict DTD. That behavior should be handled with javascript. A crudely elegant way:
    
    <a href="some.html">
       onclick="return ! window.open(this.href);">someplace</a>
    Code (markup):
    If the new window fails to open, the link will act normally and open the page in the same window. Fail safe.

    Better would be to not have any javascript in the document. It should be in a separate file.
    
    the js file
    
    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.onclick = "return ! window.open(this.href);"
          }
       }
    }
    
    window.onload = externalLinks;
    ===========
    the html
    
    <a href="some.html" rel="external">Some page</a>
    Code (markup):
    cheers,

    gary
     
    kk5st, Mar 7, 2007 IP
  4. Clive

    Clive Web Developer

    Messages:
    4,507
    Likes Received:
    297
    Best Answers:
    0
    Trophy Points:
    250
    #4
    Using javascript to force a link to open in a new window is relying on JavaScript to be enabled on the client, wow that's nice.
    Whenever I can avoid script I do so. Why would I go back to js instead of using target="_blank"? How funny can that be on browsers with javascript disabled?
    Wondering why would they drop the "target" attribute this time :rolleyes:
     
    Clive, Mar 8, 2007 IP
  5. David M

    David M Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Ive always used target blank, does the job i suppose, but a problem if as you say you want validated code.
     
    David M, Mar 8, 2007 IP
  6. Clive

    Clive Web Developer

    Messages:
    4,507
    Likes Received:
    297
    Best Answers:
    0
    Trophy Points:
    250
    #6
    The validation stuff looks fine, just that sometimes it's as if people out there keep getting bored and try reinventing the wheel :)
    target="_blank" has been tested working fine for years. Why has it become bad now?
     
    Clive, Mar 8, 2007 IP
  7. Skinny

    Skinny Peon

    Messages:
    1,864
    Likes Received:
    93
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Ya i use target="blank" in my a tag. Didn't even know the took it out. It works so I use it.

    Skinny
     
    Skinny, Mar 8, 2007 IP
  8. kk5st

    kk5st Prominent Member

    Messages:
    3,497
    Likes Received:
    376
    Best Answers:
    29
    Trophy Points:
    335
    #8
    First, let me offer a corrected js file. The one above likely won't work. My bad for just whipping it off without thinking. This is better, mostly because it works, and is unobtrusive.
    
    window.onload = function() {
       if (!document.getElementsByTagName) return false;
       var links = document.getElementsByTagName("a");
       for (var i=0; i<links.length; i++) {
          if (links[i].getAttribute("rel") == "external") {
             links[i].onclick = function() {
                return !window.open(this.href);
             }
          }
       }
    }
    Code (markup):
    The reasons the target attribute was purged from html4 are
    1. The target attribute is actually not an attribute of a, but is a behavior. In terms of best practice, behavior, like presentation, should not be mixed into the semantic and structural markup (html).
    2. The document object model (DOM) had been adopted by the UA vendors and provided a more elegant solution to adding behavior to documents.
    3. ECMAScript, what we call javascript, became standardized against the DOM, further improving the state of the art.
    4. Accessibility issues require that opening multiple instances of the UA be avoided. Most assistive technology depends on being able to disable javascript for this purpose.
    Using javascript as I've done does not require that you have javascript enabled. If you don't have it, the link works as designed, it GETs a new page in the existing window. Using js means if you want your browser to open a new window it can, and if you don't want your browser to open a new window, it won't. The target attribute does not allow the user that choice[1]. It's called progressive enhancement. You can get as fancy as your visitor will let you. I mean, why do you think you should be able to "force" a link to open a new window? It is the visitor's machine, after all.

    I don't mean to pick on you, Clive. It's just that you had the best stated objections. :D

    cheers,

    gary

    [1] my Firefox is configured to open a new tab when encountering window.open(). I like that. The target attribute opens the new window unless I think to stop it, link by link. I don't like that.
     
    kk5st, Mar 8, 2007 IP
  9. Mooseman

    Mooseman Peon

    Messages:
    453
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Just use target="_blank" in the link code, it works fine
     
    Mooseman, Mar 10, 2007 IP
  10. kk5st

    kk5st Prominent Member

    Messages:
    3,497
    Likes Received:
    376
    Best Answers:
    29
    Trophy Points:
    335
    #10
    Yes, it does, because browsers try to be backward compatible. The target attribute has been deprecated since 1999, and is not guaranteed to work in the future. If you wish to display shoddy, unprofessional workmanship, then use it, by all means. If you wish to be a professional, delivering quality work, then do it the right way.

    Your choice, shoddy or good quality workmanship.

    cheers,

    gary
     
    kk5st, Mar 10, 2007 IP
  11. rodaniel

    rodaniel Peon

    Messages:
    30
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #11
    For the longest time, I've standardized on using target="_blank" for external links only. I had started this to for "stickiness" sake - I didn't want to seem like I was encouraging people to leave my site just because I offered links to others' sites. But I'm beginning to rethink this convention...

    But the more I read about this, there's a pretty nasty backlash in the webmastering community against using targeting a new window for any links. As Gary said, it wreaks havoc for those with accessibility issues and it makes the assumption that you have the right to force a particular function on a visitor's PC.

    Definitely cause for additional thought. Being a considerate webmaster may be the difference between return visitors or not...

    Rob
     
    rodaniel, Mar 11, 2007 IP
    kk5st likes this.
  12. kk5st

    kk5st Prominent Member

    Messages:
    3,497
    Likes Received:
    376
    Best Answers:
    29
    Trophy Points:
    335
    #12
    Let me add, on the social engineering aspect, that people who browse with their UAs maximized may not realize they have a new instance of the browser. Now, for them, the back button is broken. Instead of clicking 'back', they just go somewhere else. You've lost them for sure. People know how to go back; let them do so.

    Opening a new window is good for explaining something, or pointing to errors on a form, &c.. Make the window a reduced size and make the close function easy to find and you have something that benefits the visitor rather than causing him confusion.

    cheers,

    gary
     
    kk5st, Mar 11, 2007 IP
  13. Lilyz

    Lilyz Guest

    Messages:
    17
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #13
    First I like my visitors to have the new links open in new window or tap because my site is a resource site with tons of links (of course not a link farm)
    and having the links open in the same window will effect the stats of my sites, because visitors are on my page for less than 1 minute and I have visitors exiting more than they're entering. I want visitors to have enough time to notice other quality content.
    And the reason I wanted to use the target blank in css was that my site is a blog and links are a link blog and there is not way I can access the <a> to be able to add target="_blank"
    now that this doesn't work in css, I will leave it the way it is (links opens in the same window or tab) and maybe this is for the best.
    thanks again

    cheers

    www.montrealmommy.com
     
    Lilyz, Mar 15, 2007 IP
  14. Josh Inno

    Josh Inno Guest

    Messages:
    1,623
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #14
    IE and Firefox have javascript enabled by default. If someone disables javascript, it is likely because they specifically want to avoid things like files opening in new windows. So here's the question... if you want your user to open all of your many many links in new windows, and your user doesn't want too... which is most likely to get you what you want as an END result rather than the result for a particular link?

    Do you want return visitors? Do you want users to continue to use your site?

    Opening new windows is a perfectly acceptable feature for a site... as long as a user can remove this when it is important for them, personally, to do so.
     
    Josh Inno, Mar 16, 2007 IP
    kk5st likes this.
  15. expergefacio

    expergefacio Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #15
    You can just reverse the code above writing != "self" in stead of == "external" will make every a element without "rel="self"" open in a new window/tab...

    The edited code:
    window.onload = function()
    {
       if (!document.getElementsByTagName) return false;
       var links = document.getElementsByTagName("a");
       for (var i=0; i<links.length; i++)
       {
          if (links[i].getAttribute("rel") != "self")
          {
             links[i].onclick = function()
             {
                return !window.open(this.href);
             }
          }
       }
    }
    Code (markup):
    (PS: I have no experience in JavaScript what so ever, so i don't know is this becomes "semantic," but it works fine for me)
     
    expergefacio, Dec 14, 2009 IP
  16. kk5st

    kk5st Prominent Member

    Messages:
    3,497
    Likes Received:
    376
    Best Answers:
    29
    Trophy Points:
    335
    #16
    There are two problems with that, expergefacio. One is an issue I didn't address above, which I will in a moment. The other is that your method defaults to sending the user to a new browser instance. If you fail to mark each and every link for which you don't want to open a new window, away they go.

    The other issue I failed to address two years ago is that the rel attribute, among others, allows multiple values. For example, rel="self next", or rel="external next". Your test, and mine will always test false, and the script fails with opposite results.

    I dealt with this in another thread a while back. The following code corrects the bug.
      window.onload = function() {
        var links = document.getElementsByTagName("a");
        for (var i = 0; i < links.length; i++) {
          var rels = links[i].getAttribute("rel");
          if (rels) {
            var testpattern = new RegExp("external");
            if (testpattern.test(rels)) {
              links[i].onclick = function() {
                return !window.open(this.href);  
              }
            }
          }    
        }
      }
    Code (markup):
    cheers,

    gary
     
    kk5st, Dec 14, 2009 IP
  17. expergefacio

    expergefacio Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #17
    I get your point, but in my case i cant add rel="external" to my links, cause i use a php-script to "embed" an atom feed in my page. I guess i could do it on the server side using php, and that would probably be the best solution. But I'm not skilled enough to do that. So i guess I'll have to live with this solution for now...

    Is it possible to reverse that latest script like i did with the script above, or is there not a point in doing that?


    (I'm Norwegian btw. so sorry if my English is a bit crooked)
     
    expergefacio, Dec 15, 2009 IP
  18. Stomme poes

    Stomme poes Peon

    Messages:
    3,195
    Likes Received:
    136
    Best Answers:
    0
    Trophy Points:
    0
    #18
    My understanding was that target was an anchor attribute for framed sites. It was not a behaviour but a destination. target="top" etc were destinations that were not urls (since all frames are in the same urls). Actually not terribly different from using url fragments, except those are part of the url while the frame name isn't. Blank was the way to say "do not go to a frame".

    But you can't generally force people to "notice your quality content". They'll get around to closing their browser and notice there's still a window open.

    Instead, let them choose: often when I'm on a site with a lot of links, I'll open the link in the new tab with a right click. FF does this the right way and keeps me on the current page. Opera moves my focus over to the new link, the bastard. Whatever. This way, I can choose to not get lost in a maze of links and don't have to keep hitting the back button to get back to your site.

    You know what the worst thing is? Sites who mix both. The old slidrecht brandweer site does this: sometimes you click on something and a new window pops up, so you get used to closing the window, but then another link is just normal, and you end up closing your whole browser. Drives me nuts. Like it was written by a 12-year-old with a copy of FrontPage.
     
    Stomme poes, Dec 15, 2009 IP
  19. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #19
    Exactly! TARGET was deprecated in STRICT for a reason, and using javascript to replicate something you shouldn't be doing in the first place is just more asshattery. The user wants the page to open in a new window, let them choose on their end via middle-click / control-click / right click-new window, and don't shove it down their throat with target or scripting breaking normal navigation.
     
    deathshadow, Dec 15, 2009 IP
    johneva likes this.
  20. XanderCrews

    XanderCrews Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #20
    I stumbled across this site looking to do find a solution to opening a new window or popup window and possibly using CSS.

    My situation is a little more difficult I think.
    My page brings in RSS feeds generated from a calendar application via includes.
    The rss feeds are events on campus (I work at a University) and each event is a link to details of that event within the calendar application.
    I want these links (brought in from the RSS feed via includes), to open up in a popup window or smaller new window without navigation.

    And before someone gets high and mighty on me about the use of popup windows, there is definitely a time and a place for popups or new windows. Such as...links to .pdf files or other documents, links to snippets of information that people need access to without leaving the current page, links to generated info within a web application that doesn't have navigation back to the last page (my situation). And forcing someone to use the "back" button as the only means to return to your site, is ridiculous. If you own a website, you can have it function however you want. If users don't like it, then they don't have to stay. But sometimes new windows or popups are necessary and quite helpful and actually make for a more enjoyable experience for the user.

    Now...with that said...
    Does anyone have a solution to my issue?
    I need to apply a script or whatever to my rss generated links that opens up the links in a new and smaller popup window.
    These links are dynamic and are constantly changing based on the addition of new events and the expiration of current events...so needless to say I don't have a simple <a href=... to deal with.

    Thanks guys.

    Says you.
    A webmaster should be able to present his/her site in any way he/she likes. And if new windows for .pdf's or pop up windows for specific things are desired, then they should do so.

    If the user doesn't like it, they can leave. But new windows and pop ups at the right time, are essential to providing a pleasant experience for the visitor.

    To be honest, as a webmaster, I'm getting tired of people like you trying to shove your uppity attitude down my throat and telling me how my site should and shouldn't function when you obviously can't see the whole picture when it comes to site presentation.

    Sure, new windows assigned to simple links to regular pages on the same site is a bad practice, but saying you should never under any circumstances use _blank, is just plain stupid.
     
    Last edited: Jun 2, 2010
    XanderCrews, Jun 2, 2010 IP