Open two links with one click

Discussion in 'Web Design and Development' started by carlosbutler, Sep 18, 2008.

  1. carlosbutler macrumors 6502a

    carlosbutler

    Joined:
    Feb 24, 2008
    Location:
    London City
    #1
    I am trying to create somthing which will allow me to click on one link, but it opens up to different pages. now in html i believe you can not have two href's in the same <a> tag. but is there a JS way of doing this. if not, how do you do it in flash, since the only way i can think of is using an invisible layer, but thats just one layer

    thanks
    carlos
     
  2. SrWebDeveloper macrumors 68000

    SrWebDeveloper

    Joined:
    Dec 7, 2007
    Location:
    Alexandria, VA, USA
    #2
    Javascript method:

    HTML:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Open Two Windows</title>
    
    <script language="javascript">
    <!--//
    
    function OpenTwoLinks() {
    
        var WinReference1 = window.open ("http://www.google.com","link1");
        var WinReference2 = window.open ("http://www.yahoo.com","link2");
    
    return false;
    
    }
    //-->
    </script>
    
    </head>
    
    <body>
    
    <a href="#" onClick="return OpenTwoLinks()">Open Two Links here (Disable Pop-Up Blocker for this domain)</a>
    
    </body>
    </html>
    Notes:

    This is a simplified example for demo purposes, tested and works. I used an onClick event in a standard link which calls a JavaScript function that uses window.open to open two windows. I included a reference to each window so you can in your own code communicate with the window as desired. Up to you to customize each window and use your own links. I kept it simple, in the real world I'd probably set the links in two global variables at the top of the JavaScript or whatever and pass them as arguments to the function.

    Don't forget that users can disable JS and also set pop-up blockers so this method isn't foolproof.

    On a side note, this is EXTREMELY irritating to users to even do this, so I will make an assumption you're not a porn entrepreneur and have a sound business or personal need. At least warn users to disable popups and describe the originating link to let them know two windows will open. If you ask for advice how to automate this, I might not reply because I don't give advice to kiddie scripters seeking to blast windows to unsuspecting users.

    But you get the idea, regardless of intent. I had to give the speech, it's an ethical imperative.

    -jim
     
  3. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #3
    I'll echo SrWebDeveloper's sentiment that most visitors will hate having two links open, so be careful how you use this or you'll be losing visitors.
     
  4. spaghettishark macrumors newbie

    Joined:
    Oct 2, 2008
    #4
    Just found this thread while searching how to do something similar for an art project I'm doing.

    I've gotten the suggested code to work, but what I'm wondering is there a way to designate the size of the windows you want to pop up? I had this working when I only had one window popping, but it won't work for the two.

    Here's what I'm using:
    HTML:
    
    href="#" onClick="return OpenTwoLinks(this.href, 'popupwindow', 'width=500,height=500'); return false;">
    
    

    Edit:
    Ahh, nevermind, I've got it figured out. Needed to set the size parameters in the heading part.
     

Share This Page