SitePoint Sponsor

User Tag List

Results 1 to 22 of 22

Thread: Code question

  1. #1
    SitePoint Member
    Join Date
    Jul 2011
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Code question

    How do I modify <li><a href="rules2012.pdf">Rules &amp; Regs</a></li>
    to have it open in a new window?

  2. #2
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,997
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    Hi,

    You could use the target attribute, e.g. <li><a href="rules2012.pdf" target="_blank">Rules &amp; Regs</a></li>

    However, this has been deprecated as of XHTML 1.0 Strict and XHTML 1.1. Plenty of people will also tell you shouldn't have links open new windows because it's insulting to your users (they'll open a new window if they want to) and it breaks the back button because the new tab/window doesn't retain your browsing history.

    I'm not really sure where I stand on this, but here's an interesting article on the subject if you wish to read more: http://jalada.co.uk/2011/02/10/why-y...e-instead.html

    I'd also be interested to hear other people's thoughts on this.

  3. #3
    Robert Wellock silver trophybronze trophy xhtmlcoder's Avatar
    Join Date
    Apr 2002
    Location
    A Maze of Twisty Little Passages
    Posts
    6,316
    Mentioned
    60 Post(s)
    Tagged
    0 Thread(s)
    No, not really 'deprecated' it [_blank] was meant for use alongside with 4.01, Frameset only. XHTML 1.1 is modular and it has never appeared in any Strict Doctype: http://www.w3.org/MarkUp/2004/xhtml-faq#target for obvious reasons that it is the realm of behavioural unobtrusive JavaScript.

    Recently we've had a discussion of "Separation of Concerns", as to why _blank should not be misused: #29 and #39 and why you should always inform the user of popup link functionally.

  4. #4
    Life is not a malfunction gold trophysilver trophybronze trophy
    TechnoBear's Avatar
    Join Date
    Jun 2011
    Location
    Argyll, Scotland
    Posts
    6,360
    Mentioned
    268 Post(s)
    Tagged
    5 Thread(s)
    In the case of PDFs, as in the example, different browsers/systems behave in different ways by default. On my (Linux) system, PDFs open in the document viewer, not in the browser.

  5. #5
    Robert Wellock silver trophybronze trophy xhtmlcoder's Avatar
    Join Date
    Apr 2002
    Location
    A Maze of Twisty Little Passages
    Posts
    6,316
    Mentioned
    60 Post(s)
    Tagged
    0 Thread(s)
    Precisely Bear, like I briefly mentioned in the other thread, in either-case the link could theoretically benefit from the PDF MIME hyperlink type="application/pdf" attribute for the content.

  6. #6
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,997
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    Thanks for the link to the threads concerning "_blank" xhtmlcoder.
    I've just finished reading them and that actually really helps me to clarify my opinion.
    What I'd been doing so far was using "_blank" and displaying a small symbol informing the user that the link would open in another window.
    I guess I'll rethink that now.

  7. #7
    Robert Wellock silver trophybronze trophy xhtmlcoder's Avatar
    Join Date
    Apr 2002
    Location
    A Maze of Twisty Little Passages
    Posts
    6,316
    Mentioned
    60 Post(s)
    Tagged
    0 Thread(s)
    Pullo, glad you've seen the light. At least previously you've considered informing the user the page will possibly open in a new window.

  8. #8
    It's all Geek to me silver trophybronze trophy
    ralph.m's Avatar
    Join Date
    Mar 2009
    Location
    Melbourne, AU
    Posts
    24,297
    Mentioned
    460 Post(s)
    Tagged
    8 Thread(s)
    Quote Originally Posted by vettenut View Post
    How do I modify <li><a href="rules2012.pdf">Rules & Regs</a></li>
    to have it open in a new window?
    You could use jQuery. For example, give each PDF link a rel of "pdf":

    Code:
    <a href="rules2012.pdf" rel="pdf">Rules &amp; Regs</a>
    ... and then use some jQuery like this:

    Code:
    $('a[rel="pdf"]')
    .click( function() {
    window.open( $(this).attr('href') );
    return false;
    });
    So a full example page would look like this:

    Code:
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="utf-8">
    </head>
    <body>
    
    <p><a href="rules2012.pdf" rel="pdf">Rules &amp; Regs</a></p>
    
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js"></script>
    <script>
      $('a[rel="pdf"]')
      .click( function() {
      window.open( $(this).attr('href') );
      return false;
    });
    </script>
    </body>
    </html>

  9. #9
    SitePoint Wizard bronze trophy PicnicTutorials's Avatar
    Join Date
    Dec 2007
    Location
    Carlsbad, California, United States
    Posts
    3,658
    Mentioned
    15 Post(s)
    Tagged
    0 Thread(s)
    I just use target blank. I see no reason to use js for this when target does it better. As a general rule I only open off site links in new windows if I do it. Or just recently I added a FAQs page. I have 50 click to open facts and each has an included link to more info. I used target blank there too even though they are inter site links. There is something to be said for keeping the user on your site. And not purposefully launching them off just because other say its better.

  10. #10
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,882
    Mentioned
    74 Post(s)
    Tagged
    6 Thread(s)
    Quote Originally Posted by ralph.m View Post
    You could use jQuery. For example, give each PDF link a rel of "pdf":

    Code:
    <a href="rules2012.pdf" rel="pdf">Rules & Regs</a>
    ... and then use some jQuery like this:
    ...
    ...
    ...
    [/code]
    A recent search for a similar problem revealed this elegant inline solution:
    HTML Code:
      <a href="rules2012.pdf" onclick="window.open(this.href); return false;">
          Rules & Regs
      </a>
    Learn how to be ready for The New Move to Discourse

    How to make Make Money Now with a *NEW* look

    Be sure to congratulate Wolfshade on earning Member of the Month for August 2014

  11. #11
    Robert Wellock silver trophybronze trophy xhtmlcoder's Avatar
    Join Date
    Apr 2002
    Location
    A Maze of Twisty Little Passages
    Posts
    6,316
    Mentioned
    60 Post(s)
    Tagged
    0 Thread(s)
    I shall reiterate: _blank is for use with Frameset (DUAM XNAHT). The complete misuse of _blank is not guaranteed to keep people on your site and if anything it is more likely to distract them and make them wander off somewhere else.

    The age-old generic argument "I don't want people to leave my website!" (so I'll force a popup) was the kind of craziness you saw least century... I first mistook thee for a statue, when I regarded thy head of stone.

    Off Topic:

    Note: if a user wants to leave, they're going to leave. If you want them to stay, make them want to stay. Anyone who thinks they need to force their visitors to stay on their site should focus on writing more interesting copy rather than spawning windows.

    To put it another way: if you think FONT and CENTER and U elements are COOL, then feel free to use target as well - if not, put it in an external, unobtrusive JavaScript file.

  12. #12
    SitePoint Wizard Stomme poes's Avatar
    Join Date
    Aug 2007
    Location
    Netherlands
    Posts
    10,283
    Mentioned
    51 Post(s)
    Tagged
    2 Thread(s)
    The new window thing becomes moot as more and more browsers open those links as new tabs by default anyways.

    I learned through user testing to open PDFs in new windows though. All the users we tested were generic Windows users who used IE. If we didn't abuse target or use JS to open the PDF in a new window, the users always closed the whole browser while assuming they were only closing the PDF. Every time. Luckily our test browsers remembered the last-opened windows...

    I find the idea of using outdated Frameset attributes to open new windows quite funny, because we're not using _top or any of the other frame names (though I've seen _self on websites still! Amazing).

    It's just retardation and finding out 2 hours later that you've still got a window open of a site you left 2 hours ago means, as Crusty would say, 2 things, and Jack left town.

    As a general rule I only open off site links in new windows if I do it. Or just recently I added a FAQs page. I have 50 click to open facts and each has an included link to more info. I used target blank there too even though they are inter site links.
    Thank the gawds my browser can override whatever some webbie writes. I can't think of one good reason why I would want a separate window for ANYTHING. Well, unless I was using IE6 which doesn't have tabs. New windows had more reason to exist with IE6 around.

    Quote Originally Posted by John
    A recent search for a similar problem revealed this elegant inline solution:
    I don't hate that idea as much as I used to, but if there were more than say, 3 links on the page I was doing that to, I'd still go back to an external JS file who would find all the links for me and grab their href values and whatnot. Cuz I'm lazy.

  13. #13
    Community Advisor silver trophybronze trophy
    dresden_phoenix's Avatar
    Join Date
    Jun 2008
    Location
    Madison, WI
    Posts
    2,813
    Mentioned
    34 Post(s)
    Tagged
    2 Thread(s)
    Whenever you can avoid using js do so.
    As a general rule I only open off site links in new windows if I do it. Or just recently I added a FAQs page. I have 50 click to open facts and each has an included link to more info. I used target blank there too even though they are inter site links.
    this makes perfect sense; a good example would be a web designer portfolio page where you click on an example and it sends you to the actual live site. If the user starts browsing that site.. it might be a mess of back buttons for him/her to navigate back to the porftolio. On the other hand if you are doing some 1998 crap where you open images in pop up window just to have them display bigger .. ugh... dont! you can easily use CSS to modal screen (if you dont feel like doing it yourself there are may prewritten scripts out there)

    So as with most question the answer is "it depends" on the specific specif situation.

  14. #14
    Robert Wellock silver trophybronze trophy xhtmlcoder's Avatar
    Join Date
    Apr 2002
    Location
    A Maze of Twisty Little Passages
    Posts
    6,316
    Mentioned
    60 Post(s)
    Tagged
    0 Thread(s)
    Mercy! Dost thou wish me to die of laughter? I do hope it wasn't 50 new; _blank forced spawned windows/new tabs from one (none framed) page? Pretty inefficient and bloated to say the least... Obviously that is a clear case of need for unobtrusive JS if there ever was for handling window management. I despair where are people learning to write markup nowadays, surely they don't advocate such crazy practice in HTML 4.01 Reference books or learning materials?

  15. #15
    SitePoint Wizard bronze trophy PicnicTutorials's Avatar
    Join Date
    Dec 2007
    Location
    Carlsbad, California, United States
    Posts
    3,658
    Mentioned
    15 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dresden_phoenix View Post
    Whenever you can avoid using js do so.


    this makes perfect sense; a good example would be a web designer portfolio page where you click on an example and it sends you to the actual live site. If the user starts browsing that site.. it might be a mess of back buttons for him/her to navigate back to the porftolio. On the other hand if you are doing some 1998 crap where you open images in pop up window just to have them display bigger .. ugh... dont! you can easily use CSS to modal screen (if you dont feel like doing it yourself there are may prewritten scripts out there)

    So as with most question the answer is "it depends" on the specific specif situation.
    Thank you yes I agree. There is a time and a place for. Another good example is Gmail. Gmail uses target blank. Just like my extra info 50 links, on Gmail you dont want to leave the page, you just want to see the link, click around a bit, and then come back. It works in my case. And many others. JS slows down sites. Target blank does not.

  16. #16
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,882
    Mentioned
    74 Post(s)
    Tagged
    6 Thread(s)
    When I open Sitepoint Forums I click on the "Whats New" button. Clicking on any thread opens the thread, I prefer opening the thread in a new window...
    Learn how to be ready for The New Move to Discourse

    How to make Make Money Now with a *NEW* look

    Be sure to congratulate Wolfshade on earning Member of the Month for August 2014

  17. #17
    SitePoint Wizard Stomme poes's Avatar
    Join Date
    Aug 2007
    Location
    Netherlands
    Posts
    10,283
    Mentioned
    51 Post(s)
    Tagged
    2 Thread(s)
    ...which is why browsers allow you, the user, to choose. And as I said, luckily the browser allows us to override someone else's decision they wanted to make for us.

    Ain't nothing stopping anyone from right-clicking (or CTRL-ENTERing) any link and forcing it to a new tab or whatever all by themselves.

    I must be becoming a libertarian or something o_O

  18. #18
    SitePoint Wizard bronze trophy PicnicTutorials's Avatar
    Join Date
    Dec 2007
    Location
    Carlsbad, California, United States
    Posts
    3,658
    Mentioned
    15 Post(s)
    Tagged
    0 Thread(s)
    The thing is, as often is, with people smarter than average, you forget that 90% of the people out there would forget how to wipe their ass if they didn't get a constant reminder each day, much less remember how to work a computer. I am in the unique position of being of average intelligence, which gives me a clearer view of the average user. And the average user is who I code my site for. Not the uber-nerds.

    Edit. However on principal I agree with you. I don't like to be told what to do either.

  19. #19
    Robert Wellock silver trophybronze trophy xhtmlcoder's Avatar
    Join Date
    Apr 2002
    Location
    A Maze of Twisty Little Passages
    Posts
    6,316
    Mentioned
    60 Post(s)
    Tagged
    0 Thread(s)
    I don't think anyone is arguing that in some instances a new window would be good; it's more the case of how the author writes the method or (informs or doesn't) the user that the link has been designed to open in such a way. That is the major point repeatability being made.

    Quote Originally Posted by Robert Wellock View Post
    Off Topic:

    Therefore point proved the forum mutilated the hyperlink in the second example adding target _blank and forced a window to be spawned just to link to a post higher up the page. Argh! So just use your imagination (second hyperlink) and assume it was coded: <a href="somelink">...</a> as would have been the case if unobtrusive JavaScript had have been used.
    A truly idiotic solution by the forum software in action.

    In a secured control panel session 'user login' type environment then it theoretically could make sense for the prevision for unobtrusive JS to open some control toolbox windows. Certainly if I had to suffer five or more new windows forced by _blank it would probably overload my memory, I have of course a disability and it'd probably result in my closing them all.

    The JS processing cycles to open a new window would be negligible in this instance. The actual opening of the new window/tab by any method is what would consume resources and when you abuse target and let's say to have 3 windows open that's 2 (orphans) you'd need to close. Whereas with in contrast unobtrusive JS you could keep it manageable or even in absence of JS the same applies - easy management allowing the user to deicide.

    Poes, is an example of a dumb user so if a stupid cat wants choice; then it's clear unobtrusive JS is the right choice, as far as the visitor is concerned.

  20. #20
    It's all Geek to me silver trophybronze trophy
    ralph.m's Avatar
    Join Date
    Mar 2009
    Location
    Melbourne, AU
    Posts
    24,297
    Mentioned
    460 Post(s)
    Tagged
    8 Thread(s)
    I agree with poes that the fact that many browsers open a new window in a new tab makes this less of an issue than it once was; and I also click command/control to open links in new tabs ... but most people probably don't know about that option. At the end of the day, target was deprecated for a reason, but I guess it was also re-introduced in that bad-boy HTML5 for a reason, too ...

    I still see my mother get confused when a page opens in a new tab (I've seen her fumbling for the back button and not sure why it's not working ... and then closing the browser altogether ...) so I'm still not keen on the target attribute. I've tried to teach her how tabs work, but it's a slog.

  21. #21
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,997
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    Quote Originally Posted by ralph.m View Post
    I still see my mother get confused when a page opens in a new tab (I've seen her fumbling for the back button and not sure why it's not working ... and then closing the browser altogether ...) so I'm still not keen on the target attribute. I've tried to teach her how tabs work, but it's a slog.
    I feel your pain on that one Ralph, it's the same story at work.
    I've shown several colleagues how tabbed browsing works and while some have comprehended it, others still get confused and end up shutting their browsers after I've opened more than one tab for them, as they forget how to get back to where they were.
    To bring this back to the discussion at hand, I guess it strengthens the point that you shouldn't force the user to do something they don't want / are not expecting.

  22. #22
    SitePoint Wizard bronze trophy PicnicTutorials's Avatar
    Join Date
    Dec 2007
    Location
    Carlsbad, California, United States
    Posts
    3,658
    Mentioned
    15 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Pullo View Post
    I feel your pain on that one Ralph, it's the same story at work.
    I've shown several colleagues how tabbed browsing works and while some have comprehended it, others still get confused and end up shutting their browsers after I've opened more than one tab for them, as they forget how to get back to where they were.
    To bring this back to the discussion at hand, I guess it strengthens the point that you shouldn't force the user to do something they don't want / are not expecting.
    When a new window opens focus remains on the original window. The user has to physically move their mouse over to the new tab and click in order to view it. If they don't know how to do the same to view the original window then they have bigger problems. That said, yes I agree with both, my mom always has 10 windows with 10 tabs going on in each. Not because she's an awesome multitasker, but because she simply never closes a single window.


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
  •