www.webdeveloper.com
Results 1 to 14 of 14

Thread: Setting the target of a certain area?

  1. #1
    Join Date
    Mar 2010
    Posts
    701

    Setting the target of a certain area?

    I want all the links in a certain area to be target="_blank". How do I do that? body target="_blank" doesn't work because it sets the whole page to _blank.

  2. #2
    Join Date
    Dec 2005
    Posts
    2,984
    Javascript, perhaps - get each anchor in a certain area and set this attribute.

    But I would argue it's better to just set target="_blank" on all elements, just for the rare users who aren't using Javascript.
    Doesn't make sense to come up with a JS solution and then add target="_blank" to all elements anyway to support JS-disabled visitors.

  3. #3
    Join Date
    Mar 2010
    Posts
    701
    I have a blog where users can submit posts. I want all the links in the posts to be _blank

  4. #4
    Join Date
    Mar 2010
    Posts
    2,803
    Quote Originally Posted by narutodude000 View Post
    How do I do that?
    I would do it using javascript.

  5. #5
    Join Date
    Mar 2010
    Posts
    701
    How? Can you provide a short example?

  6. #6
    Join Date
    Jan 2009
    Posts
    3,346
    Quote Originally Posted by narutodude000 View Post
    I have a blog where users can submit posts. I want all the links in the posts to be _blank
    Is your blog powered by a server-side language? If so, use it to append target="_blank" to the appropriate links.

  7. #7
    Join Date
    Dec 2005
    Posts
    2,984
    As I said in my first post, and criterion also reiterated - just append target="_blank" to the links. Doing it with Javascript is a waste of time. Appending target="_blank" is fast (I am assuming your blog is probably powered with wordpress in which case you just need to change one line) versus doing it with JS you need to write a script, and you'll won't achieve the desired effect when people without JS visit.

  8. #8
    Join Date
    Jan 2009
    Posts
    3,346
    Quote Originally Posted by aj_nsc View Post
    As I said in my first post, and criterion also reiterated - just append target="_blank" to the links. Doing it with Javascript is a waste of time. Appending target="_blank" is fast (I am assuming your blog is probably powered with wordpress in which case you just need to change one line) versus doing it with JS you need to write a script, and you'll won't achieve the desired effect when people without JS visit.
    I was suggesting automating it. I would suggest using _blank only for hrefs that are off-domain though. A regex would make quick work of that and you wouldn't have to remember to change the target manually that way.

  9. #9
    Join Date
    Nov 2002
    Location
    Baltimore, Maryland
    Posts
    12,279
    JavaScript is way easier. And those of us without JavaScript, we don't want to be bothered with new windows either.
    “The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect.”
    —Tim Berners-Lee, W3C Director and inventor of the World Wide Web

  10. #10
    Join Date
    Jan 2009
    Posts
    3,346
    Quote Originally Posted by Charles View Post
    JavaScript is way easier. And those of us without JavaScript, we don't want to be bothered with new windows either.
    You could use the same regex on the server or with Javascript. I'd say both would equally easy it just depends on how many users the OP wants to force to a new window.

  11. #11
    Join Date
    Nov 2002
    Location
    Baltimore, Maryland
    Posts
    12,279
    Except that I wouldn't use a RegExp. Those things aren't well suited to HTML and, more importantly, this is way easy with DOM methods.
    “The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect.”
    —Tim Berners-Lee, W3C Director and inventor of the World Wide Web

  12. #12
    Join Date
    Mar 2010
    Posts
    701
    Eventually, my website might be like Twitter, except smaller. I won't be able to change all the links one by one. I'd need to make all the links into _blank automatically.

  13. #13
    Join Date
    Mar 2010
    Posts
    701
    Help? Anyone?

  14. #14
    Join Date
    Nov 2002
    Location
    Baltimore, Maryland
    Posts
    12,279
    What part of "use JavaScript" do you not understand? But if you are trying to change all of them and not a subset then just use the BASE element.

    Code:
    <script type="text/javascript">
    onload = function () {for (i = 0; i < document.links.length; i++) document.links[i].target = 'foo'}
    </script>
    Last edited by Charles; 05-25-2010 at 11:54 AM.
    “The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect.”
    —Tim Berners-Lee, W3C Director and inventor of the World Wide Web

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles