#1
  1. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Nov 2001
    Posts
    1,882
    Rep Power
    20

    Base Target using Javascript


    Hi,
    Is there a way to have all offsite links open in a new window?

    Links not at http://www.forum_domainname.com will have target="_blank" applied.

    The pages are on a Remotely Hosted Forum.
    All URL's are absolute.
    All I can do is add Javascript to the Header or Footer.


    Cheers,
    Degs
    Last edited by degsy; September 22nd, 2002 at 04:22 PM.
  2. #2
  3. No Profile Picture
    Senior Citizen
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jan 2001
    Location
    leftcoast
    Posts
    2,019
    Rep Power
    16
    degsy...

    In <head>er:

    <script type="text/javascript" language="javascript">

    function setTargets() {
    var link, l = 0;
    while (link = document.links[l++]) {
    if (link.href.indexOf('http://www.forum_domainname.com') == -1)
    link.target = '_blank';
    }
    }

    </script>

    Stick this at the bottom of the file, after all link HTML:

    <script type="text/javascript" language="javascript">

    setTargets();

    </script>

    You can also run it onload, if preferable.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Nov 2001
    Posts
    1,882
    Rep Power
    20
    I've ran into a problem.
    I've just realised that the forum URL's are in the form of
    http://forumdomain.com/forumname

    e.g.
    http://forumdomain.com/forumname1
    http://forumdomain.com/forumname2
    http://forumdomain.com/forumname3

    and the posts are
    http://forumdomain.com/forumname.post1
    http://forumdomain.com/forumname.post2
    http://forumdomain.com/forumname.post3


    Can I change the URL in the script so that it works with this format.
    Possibly using the .length method?
  6. #4
  7. No Profile Picture
    Senior Citizen
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jan 2001
    Location
    leftcoast
    Posts
    2,019
    Rep Power
    16
    degsy...ya lost me...what posts?

    OK; assuming the 'posts' are the 'offsite' links...

    if (link.href.indexOf('.post') != -1)

    Something like that. Some real sample urls would help (if those aren't).
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Nov 2001
    Posts
    1,882
    Rep Power
    20
    Ok,
    The board is at
    http://pub53.ezboard.com/buhelpcanhelpforum


    The forum URL's then differ slightly.
    e.g.
    The Notices and Announcement forum URL is
    http://pub53.ezboard.com/fuhelpcanhelpforumfrm23

    Then a Topic is
    http://pub53.ezboard.com/fuhelpcanhelpforumfrm23.showMessage?topicID=41.topic



    What I would like is that all URL's that are not part of those 3 formats to open in a New Window.
    So a link posted to http://www.google.com would open a new window.


    This is default for several messageboard programs, but not for ezboard
  10. #6
  11. No Profile Picture
    Senior Citizen
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jan 2001
    Location
    leftcoast
    Posts
    2,019
    Rep Power
    16
    Maybe I'm misunderstanding...can't be this simple:

    <html>
    <head>
    <script type="text/javascript" language="javascript">

    function setTargets() {
    var link, l = 0;
    while (link = document.links[l++]) {
    if (link.href.indexOf('uhelpcanhelpforum') == -1)
    link.target = '_blank';
    }
    }

    onload = setTargets;

    </script>
    </head>
    <body>
    <a href="http://pub53.ezboard.com/buhelpcanhelpforum">board</a><br>
    <a href="http://pub53.ezboard.com/fuhelpcanhelpforumfrm23">n & a</a><br>
    <a href="http://pub53.ezboard.com/fuhelpcanhelpforumfrm23.showMessage?topicID=41.topic">topic</a><br>
    <a href="http://www.google.com">google</a>
    </body>
    </html>
    Last edited by adios; September 23rd, 2002 at 01:08 PM.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Nov 2001
    Posts
    1,882
    Rep Power
    20
    Yes, but alot of other forums are hosted on ezboard.
    I would also class them as "offsite" links.


    So i would like the script for anything with
    uhelpcanhelpforum
    (buhelpcanhelpforum or fuhelpcanhelpforum)



    Thanks for persisting with this
  14. #8
  15. No Profile Picture
    Senior Citizen
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jan 2001
    Location
    leftcoast
    Posts
    2,019
    Rep Power
    16
    You're a nice chap, no problem. Edited the above, tell me if I'm close...

    rob
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Nov 2001
    Posts
    1,882
    Rep Power
    20
    Works perfectly.
    Although I have found some other problems implementing it, but I'll have a play around with it.


    For 2 URL's I could use this?
    <script type="text/javascript" language="javascript">

    function setTargets() {
    var url1 = 'uhelpcanhelpforum'
    var url2 = 'another_url'
    var link, l = 0;
    while (link = document.links[l++]) {
    if (link.href.indexOf(url1) == -1 || link.href.indexOf(url2) == -1)
    link.target = '_blank';
    }
    }

    onload = setTargets;

    </script>


    Thanks for your time.
  18. #10
  19. No Profile Picture
    Senior Citizen
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jan 2001
    Location
    leftcoast
    Posts
    2,019
    Rep Power
    16
    My final offer :


    <html>
    <head>
    <script type="text/javascript" language="javascript">

    var checkURLs = new Array(
    'uhelpcanhelpforum' ,
    'another_url' ,
    'yet_another_url'
    );

    function setTargets() {
    var link, checkit, i, l = 0;
    while (link = document.links[l++]) {
    i = 0;
    while (checkit = checkURLs[i++])
    if (link.href.indexOf(checkit) != -1) break;
    if (i > checkURLs.length) link.target = '_blank';
    }
    }

    onload = setTargets;

    </script>
    </head>
    <body>
    <a href="http://pub53.ezboard.com/buhelpcanhelpforum">-1-</a><br>
    <a href="http://www.google.com/another_url">-2-</a><br>
    <a href="http://www.google.com/yet_another_url">-3-</a><br>
    <a href="http://www.google.com">google</a>
    </body>
    </html>
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Nov 2001
    Posts
    1,882
    Rep Power
    20
    Cool
    That'll do nicely

    Cheers,
    Degs

IMN logo majestic logo threadwatch logo seochat tools logo