SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Dec 2002
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Opening new browser windows

    I have a question about opening new pages when a link is clicked on a webpage. What I want to happen is this:

    On the main webpage, I would like to have links to other websites. For example:
    Link 1
    Link 2
    Link 3
    Now, if I was to click links 1,2 and 3, I would have 4 browser windows open: The window containing the original webpage, plus the 3 windows that were opened by clicking on the 3 links. (I am using a href="www.,,,,," target="_blank"></A>)
    Now, what I want to happen is this, I only want the mainpage to have ONE child window so that at any given time, there will be at most 2 browser windows open, the main page window and one child that is opened by clicking on a link. If I click on Link 1 followed immediately by Link 2, I don't want 2 child windows to pop up. What I need to happen is when I click Link 1, it needs to open a window. When I click link 2, the browser window that contains the webpage from link 1 now needs to show the webpage that from link 2. Is there a way to do this using javascript window.open?
    Something like this would be best:
    <A href="javascript:void(window.open('newlink.html'))">LINK</A>

    I know this can be done simply buy using a targe like the following:
    <a href="1.html" target="_mywindow">my link 1</a>,
    <a href="1.html" target="_mywindow">my link 2</a>,
    <a href="1.html" target="_mywindow">my link 3</a>,

    Unfortunately I need a way to do this using javascript or code that can be coded directly in the href=" " section of code. Any suggestions?
    Thanks in advance for any help.

  2. #2
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi backslash,

    This works even with javascript disabled:

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>winOpen Demo</title>
    <style type='text/css'><!--
    body {
      color:#000; background:#fff; margin:10px; padding:0;
      font-family:verdana,arial,sans-serif; font-size:12px;
    }
    a {
      color:#008; background:transparent; text-decoration:underline;
    }
    a:hover {
      color:#008; background:#ccc; text-decoration:none;
    }
    --></style>
    <script type='text/javascript'><!--
    var childWindow = null;
    function winOpen(pageUrl) {
      if (childWindow && !childWindow.closed) {
        childWindow.location.href = pageUrl;
        childWindow.focus();
      }
      else {
        childWindow = window.open(pageUrl, "myWinName");
      }
      return false;
    }
    //--></script>
    </head>
    <body>
    <h2>winOpen() function</h2>
    <p><i>Open the first link in a new window,
    and all subsequent links in that window.</i></p>
    <p>Test the winOpen function with these links:</p>
    <p><a target='myWinName' href='http://www.sitepoint.com/'
      onclick="return winOpen('http://www.sitepoint.com/')">SitePoint Home</a></p>
    <p><a target='myWinName' href='http://www.google.com/'
      onclick="return winOpen('http://www.google.com/')">Google</a></p>
    <p><a target='myWinName' href='http://www.sitepointforums.com/'
      onclick="return winOpen('http://www.sitepointforums.com/')">SitePoint Forums</a></p>
    </body>
    </html>
    If you have to use only the href attribute, then the links will look like the following (and let winOpen return nothing). Keep in mind the link won't work with javascript disabled.

    Code:
    <a href="javascript:winOpen('http://www.sitepoint.com/')">SitePoint</a>


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
  •