Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New Coder
    Join Date
    Jul 2009
    Posts
    36
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Closing all opened javascript windows

    Hello. I am trying a new programming technique for me. I have a website, where I keep the parent window open, and have a few buttons that open up java-script popup windows. I use a separate .js file to contain all of my java-script, and load it up as an include file. This works great. All the windows needed popup, and i can control them as far as opening them up etc. However, the issue is when I use the log-out function, I want to be able to destroy my php session and close down all the popup windows that might be open. Here is the function in my menu to open the popups, which works:

    PHP Code:
    <ul>            
         <
    li><input type="button" onClick="myPopup1(1)" value="Inventory Lookup" class="menubutton" id="menubutton"></li>
         <
    li><input type="button" onClick="myPopup2(2)" value="Order Entry" class="menubutton" id="menubutton"></li>
         <
    li><input type="button" onClick="myPopup3(3)" value="Customer Lookup" class="menubutton" id="menubutton"></li>
         <
    li><input type="button" onClick="myPopup4(4)" value="Message System" class="menubutton" id="menubutton"></li>
         <
    li><input type="button" onClick="myPopup5(5)" value="System Logout" class="menubutton" id="menubutton"></li>            
        </
    ul
    I do it this way because I need to keep the pop-ups open and separate without overwriting each window. The user can manually close the popup if they need to, but I have other plans for the popups as well.

    Here is the java-script code that does the functions:
    Code:
    <script type="text/javascript">
    <!--
    function myPopup1() {
    window.open( "includes/invent.html", "myWindow1", "status = 1, height = 400, width = 300, resizable = 0, scrollbars=yes" )
    }
    
    function myPopup2() {
    window.open( "includes/order.html","myWindow2", "status = 1, height = 400, width = 300, resizable = 0" )
    }
    
    function myPopup3() {
    window.open( "includes/customer.html","myWindow3", "status = 1, height = 400, width = 300, resizable = no, scrollbars=yes" )
    }
    
    function myPopup4() {
    window.open( "includes/message.html","myWindow4", "status = 1, height = 400, width = 300, resizable = no, scrollbars=yes" )
    }
    
    function myPopup5() {
    window.open( "includes/logout.php","myWindow5", "status = 1, height = 400, width = 300, resizable = no, scrollbars=no" )
    }
    
    //-->
    </script>
    Now I have researched how to close all pop-up windows at once, and have become confused. My need is to destroy the php session, which I know how to do, and then close any open pop-up windows. I know how to close an individual popup window from inside the window by using the window.close() function, but how do I close all windows from a separate function, and then redirect the parent window back to itself? I use a javascript redirect of:

    Code:
    <script type="text/javascript">
    window.location.href='http://bussapp.iceregent.com';
    </script>
    That works great to redirect, but here is what I have tried so far for a function to close all windows:

    PHP Code:
    <?php 
    session_start
    ();
    ?>
    <script type="text/javascript">
    myWindow1.close();
    myWindow2.close();
    myWindow3.close();
    myWindow4.close();
    myWindow5.close();
    </script>
    <?php
    session_destroy
    ();
    ?>
    <script type="text/javascript">
    window.location.href='http://bussapp.iceregent.com';
    </script>
    I read somewhere about giving the pop-ups names, but I thought I had named them individually in my java-script etc. So where do I go from here?

    Thank you;
    Ice

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,471
    Thanks
    0
    Thanked 634 Times in 624 Posts
    Unmess you give the popul windows names in the JavaScript you will have no way to reference themfrom the JavaScript to be able to close them from JavaScript.

    Perhaps the easiest way to handle it since all you want to be able to do is to close all the open ones is to assign them all into an array as they are opened. You can then simply close all the windows in the array when you need to.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #3
    New Coder
    Join Date
    Jul 2009
    Posts
    36
    Thanks
    1
    Thanked 0 Times in 0 Posts
    uhm.. Ok, not sure how to assign them all to an array. But, on the other hand, how and where do I give them names, and, how do I close a window using a name?

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,471
    Thanks
    0
    Thanked 634 Times in 624 Posts
    First you need to create the array:

    Code:
    var openWins = [];
    Then you need to amend the window.open calls to add them to that array:

    Code:
    function myPopup1() {
    openWins.push(window.open( "includes/invent.html", "myWindow1", "status = 1, height = 400, width = 300, resizable = 0, scrollbars=yes" ))
    }
    Then when you want to close all the opened windows:

    Code:
    for (var i = openWins.length-1; i >= 0; i--) openWins[i].close();
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #5
    New Coder
    Join Date
    Jul 2009
    Posts
    36
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Ok, I see your reply and understand. However, I need to know which sections each set of code needs to modify. var openWins = []; goes in which section? I assume the function code is modified in my java-script section. And I think the closing code must go into my java-script on my closing log-out form?

    Thanks;
    Ice

  • #6
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,471
    Thanks
    0
    Thanked 634 Times in 624 Posts
    Defining the array goes at the start of your JavaScript so that the array exists before any windows are opened.

    The code to close the windows needs to be called when you want them closed.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #7
    New Coder
    Join Date
    Jul 2009
    Posts
    36
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Ok, I obviously did something incorrect. Here is what I did. I modified the java-script file to show like this:

    Code:
    <script type="text/javascript">
    <!--
    var openWins = [];
    
    function myPopup1() {
    openWins.push(window.open( "includes/invent.html", "myWindow1", "status = 1, height = 400, width = 300, resizable = 0, scrollbars=yes" ))
    }
    
    function myPopup2() {
    openWins.push(window.open( "includes/order.html","myWindow2", "status = 1, height = 400, width = 300, resizable = 0" ))
    }
    
    
    function myPopup3() {
    openWins.push(window.open( "includes/customer.html","myWindow3", "status = 1, height = 400, width = 300, resizable = no, scrollbars=yes" ))
    }
    
    function myPopup4() {
    openWins.push(window.open( "includes/message.html","myWindow4", "status = 1, height = 400, width = 300, resizable = no, scrollbars=yes" ))
    }
    
    function myPopup5() {
    openWins.push(window.open( "includes/logout.php","myWindow5", "status = 1, height = 400, width = 300, resizable = no, scrollbars=no" ))
    }
    
    //-->
    </script>
    I also modified my closing script to read like this:

    Code:
    <?php 
    session_start();
    session_destroy();
    ?>
    <script type="text/javascript">
    for (var i = openWins.length-1; i >= 0; i--) openWins[i].close();
    </script>
    
    <script type="text/javascript">
    window.location.href='http://bussapp.iceregent.com';
    </script>
    It does not work. Possibly it might not work from another pop-up window? The logout function runs from this line:

    PHP Code:
    <li><input type="button" onClick="myPopup5(5)" value="System Logout" class="menubutton" id="menubutton"></li
    That is in the include file menu.php. I run a skeleton index.php, and modularize all the segments with include files.

    Ice

  • #8
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    openWins is not currently available to the script that is run from your final php page.

    Just speculating, but in the pop-up that is used to logout:
    Code:
    keepOpenWins = opener.openWins; // storing the variable (globally) from the main window
    then in your PHP script:
    Code:
    <script type="text/javascript">
    var kept = opener.keepOpenWins;
    for (var i = kept.length-1; i >= 0; i--) kept[i].close();
    </script>
    I'm speculating (a little..) though, as I haven't had a chance to test this - and it's late.
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #9
    New Coder
    Join Date
    Jul 2009
    Posts
    36
    Thanks
    1
    Thanked 0 Times in 0 Posts
    AndrewGSW<== Let me see if I can wrap my head around what you just suggested. I know more of PHP and html and css, but java-script is new to me. Thank you let me see what I can do with what you wrote...

    Ice


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •