Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-16-2012, 08:02 PM   PM User | #1
iceregent
New Coder

 
Join Date: Jul 2009
Posts: 28
Thanks: 1
Thanked 0 Times in 0 Posts
iceregent is an unknown quantity at this point
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
iceregent is offline   Reply With Quote
Old 12-16-2012, 08:25 PM   PM User | #2
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,451
Thanks: 0
Thanked 496 Times in 488 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
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/
felgall is offline   Reply With Quote
Old 12-16-2012, 08:28 PM   PM User | #3
iceregent
New Coder

 
Join Date: Jul 2009
Posts: 28
Thanks: 1
Thanked 0 Times in 0 Posts
iceregent is an unknown quantity at this point
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?
iceregent is offline   Reply With Quote
Old 12-16-2012, 08:48 PM   PM User | #4
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,451
Thanks: 0
Thanked 496 Times in 488 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
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/
felgall is offline   Reply With Quote
Old 12-16-2012, 09:28 PM   PM User | #5
iceregent
New Coder

 
Join Date: Jul 2009
Posts: 28
Thanks: 1
Thanked 0 Times in 0 Posts
iceregent is an unknown quantity at this point
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
iceregent is offline   Reply With Quote
Old 12-16-2012, 09:33 PM   PM User | #6
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,451
Thanks: 0
Thanked 496 Times in 488 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
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/
felgall is offline   Reply With Quote
Old 12-16-2012, 09:40 PM   PM User | #7
iceregent
New Coder

 
Join Date: Jul 2009
Posts: 28
Thanks: 1
Thanked 0 Times in 0 Posts
iceregent is an unknown quantity at this point
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
iceregent is offline   Reply With Quote
Old 12-16-2012, 11:58 PM   PM User | #8
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
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
AndrewGSW is offline   Reply With Quote
Old 12-17-2012, 02:12 AM   PM User | #9
iceregent
New Coder

 
Join Date: Jul 2009
Posts: 28
Thanks: 1
Thanked 0 Times in 0 Posts
iceregent is an unknown quantity at this point
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
iceregent is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:46 AM.


Advertisement
Log in to turn off these ads.