SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    Join Date
    Mar 2005
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    closing popups on return to main window

    I'm trying to figure out how to close a popup window automatically when the viewer returns to the main window.

    I'm presenting a series of composite pages from a drop down menu and right now all I've succeeding in doing is bringing the popup to the front when it is first opened.

    If I go back to the main window and choose another link, the popup stays in the background. I don't want to open a popup for each link and if all else fails, I'd be happy with using one window if I could bring it back to focus when it is loaded.

    Ideally, I'd like the popup to close when the viewer clicks back on the main window but so far no joy. I can close the popup with a link, no problem, but I'd like to do it instinctively.

    Is there any way to close a window when it loses focus?

    If not, how do i refocus the window when a new link is selected?


    popup script is conventional, like so:

    Code:
    function popup(mylink, windowname)
    {
    if (! window.focus)return true;
    var href;
    if (typeof(mylink) == 'string')
       href=mylink;
    else
       href=mylink.href;
    window.open(href, windowname, 'width=600,height=420,top=50,left=100,scrollbars=no,menubar=no');
    return false;
    }
    Hope someone has the time to help.
    cheers

  2. #2
    Non-Member
    Join Date
    Jan 2005
    Location
    Netherlands
    Posts
    4,300
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello

    This is what i have got in my achieves:

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <title>12345678 12345678 12345678 12345678</title>
    <script type="text/javascript">
    <!--
      rc_win=null;
      function start_rc() {
        rc_win=window.open("","rc_win","directories=0,location=0,menubar=0,resizable=1,scrollbars=0,status=0,toolbar=0,width=600,height=420,top=50,left=100");
        if(rc_win != null) {
          if(rc_win.opener == null)rc_win.opener=self;
          href=window.location.href.toLowerCase();
          href=href.substring(0, href.indexOf("page"));
          rc_win.location.href=href+"index.htm";
        } else {
          alert("this is going wrong");
        }
      }
      function stop_rc() {
        if(rc_win != null) {
          rc_win.close();
          rc_win=null;
        }
      }
    //-->
    </script> 
    </head>
    <body onfocus="stop_rc()">
    <a href="##" onclick="start_rc();">start</a><br>
    <a href="##" onclick="stop_rc();">stop</a><br><br>
    
    or use a time out in popup:
    <!--<script type="text/javascript">
    
      var sec=5;   // time out
      window.setTimeout("window.close()",(sec*1000) );
    
    </script>//-->
    
    </body>
    </html>

  3. #3
    SitePoint Member
    Join Date
    Mar 2005
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for that. It works and I can use it on the page. Unfortunately, I can't get it to work within a drop down menu.

    revised menu code is this

    Code:
    <select name="cocktail" onChange="start_rc();" 
          <option value="BML/baileys.html" selected>Baileys Comet</option>
          <option value="BML/coldsplash.html">Colada Splash</option>
          <option value="BML/cosmicolada.html">Cosmic Colada</option>
          <option value="BML/eclipse.html">Eclipse</option>
          <option value="BML/fozenmarg.html">Frozen Margarita</option>
          <option value="BML/gingermartini.html">Ginger Martini</option>
          <option value="BML/illusion.html">Illusion</option>
          <option value="BML/troopmoon.html">Tropical Moon</option>
        </select>
    All it will do is call up the file index.htm which is referenced in the rc_win code


    I can't figure out how to replace the index.htm with the url value from the menu list, have tried different ways but I'm lost at how to create a variable within the option list and then read it into the function.

    Is there an easy way to do this or should I be trying to create my own drop down menu using js and creating a variable there?

    sorry if I'm missing something obvious here but I'm stumped.

  4. #4
    Non-Member
    Join Date
    Jan 2005
    Location
    Netherlands
    Posts
    4,300
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello

    The script only starts / closes a seperate browser window, if you want to contol items by javascript the script normaly hase to be in the HTML page of the items, you can however control item's from te popup to the starting window.

    I think you need an second different script

  5. #5
    SitePoint Member
    Join Date
    Mar 2005
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I see your point. I will experiment.

    Thanks

  6. #6
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is there any way to close a window when it loses focus?
    You can use the onblur event. Put this between some <script> tags in the <head> of your popup:

    window.onblur = function(){window.close()};

  7. #7
    Non-Member
    Join Date
    Jan 2005
    Location
    Netherlands
    Posts
    4,300
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello

    You need something like this: ??

    http://lab.artlung.com/scripting/dropdown/

    http://artlung.com/lab/scripting/dro...ome-new-window


    Then use 7stud's small code in popup
    window.onblur = function(){window.close()};


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
  •