SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Wizard
    Join Date
    Oct 2005
    Location
    London
    Posts
    1,678
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    opening external links in one popup window only

    Hi,

    I think i've seen this working before so i'm sure it can be done and I just can;t think of how to do it.

    We have a site that has lots of external links. They open up in a new window...what I want to do is not open a new window everytime a new external link is clicked but just load it in the previously opened window....if there isn't another window open then open one.

    How do i do this?

    Thanks

  2. #2
    SitePoint Addict
    Join Date
    Jun 2006
    Location
    Durban, South Africa
    Posts
    287
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    When you use the window.open method, the second argument is a name. IF you want to open all the links in the same window, just use the same name

    Example

    [code]
    <span onclick="temp();">temp</span>
    <span onclick="temp2();">temp2</span>

    <script type="text/javascript">
    function temp() {
    window.open("http://www.google.co.za" , "win1");
    }

    function temp2() {
    window.open("http://www.microsoft.com" , "win1");
    }
    </script>
    [code]

    Hope this helps
    Cheers

  3. #3
    SitePoint Wizard
    Join Date
    Oct 2005
    Location
    London
    Posts
    1,678
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    great...that's the info i was after....i'd like to take that and make it so that all that needs to be done is to add a small script in the head and then just give links that need to be opened in an external window a class....all that have this class get opened by the script. I can do this fine....but i'm not sure how to prevent the default action of the link.....I've only ever done it when i've coded the javascript inline inside the link.......return flase does the trick....but how do i stop the link being followed when i'm using external javascript? Mayvbe i can return false out of the script....I know javascript is always run before the default behaviour so it might work?!

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Code javascript:
    function openExternal() {
        window.open(this.href, 'name');
        return false;
    }
    function initExternalLinks() {
        var els = getElementsByClassName('external', 'a'),
            el,
            i;
        for (i = 0; i < els.length; i += 1) {
            els[i].onclick = openExternal;
        }
    }
    initExternalLinks();

    You also want a getElementsByClassName function, perhaps from http://www.robertnyman.com/2008/05/2...ame-anno-2008/
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    SitePoint Wizard
    Join Date
    Oct 2005
    Location
    London
    Posts
    1,678
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Aha, Nice one. I've got a similar script set up...was just double checking on the default behaviour bit.

    Thanks for your help on this and my other post this week


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
  •