Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Closing a window (in AJAX callback) 2

Status
Not open for further replies.

Borvik

Programmer
Jan 2, 2002
1,392
US
I'm creating a web application using AJAX (Sajax PHP library).

To allow a user to add a category to a select box, I have a link trigger a new window to open via window.open().

This window collects the information, and submits it to a function that calls the add routine via AJAX. In the callback method - if the add routine was successful, it closes the window that was opened.

Everything works fine, except for the closing of the window - which is acting strangely. It doesn't close right away, I have to either click on the document area of the opened window, or switch focus to another window and back again.

Here is the code in the callback method (and also the opening function as well):

Code:
		var popupWins = new Array();
		function nw(obj,name,w,h){
			if( typeof w == "undefined" )
				w = 500;
			if( typeof h == "undefined" )
				h = 500;
			leftVal = (screen.width - w) / 2;
			topVal = (screen.height - h) / 2;
			var attrS = 'left=' + leftVal + ',top=' + topVal + ',width=' + w + ',height=' + h;
			popupWins[name] = window.open(obj.href, name, attrS);
			if( window.focus ){ popupWins[name].focus(); }
			return false;
		}

		function addPlatform_cb(z){
			z = parseInt(z);
			if( z > 0 ){
				window.focus();
				popupWins['new_entry'].close();
				updateDisplay(platform);
			}else
				popupWins['new_entry'].error('Platform "{0}" could not be created.');
		}

Anyone have any ideas?
 
Oh, and by the way - I should mention this works fine in IE7, but I get the aforementioned problem using FireFox 2.
 
have you tried having the popup window call the ajax and handle the return, and then you can use self.close();

if not it might require parent.windowname.close();

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
I have tried sticking a function in the popup window called closeme that called self.close() - so I had
Code:
popupWins['new_entry'].closeme();
instead of
Code:
popupWins['new_entry'].close();

The AJAX callback should really stay in the parent window, as it also refreshing the display.
 
did this work?

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
No it didn't work.

I created a test page that create a popup window in the same way, and gave that popup window three ways to close (self.close, parent calls child self.close, parent closes) and it worked just fine.

I'm going to have to say my problem has something to do with the fact it's coming from the callback of an AJAX method. How to fix it - I still don't know.
 
i've found that buttons with window.close(); or self.close(); don't always close the window in FF, maybe they have a problem with over zelous javascript security.



"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
Try this, reestablish the link from the parent window to the child.

Code:
        function addPlatform_cb(z){
            z = parseInt(z);
            [!]popupWins['new_entry'] = window.open("whateverTheURLofthepopupis", "new_entry", "");[/!]
            if( z > 0 ){
                window.focus();
                popupWins['new_entry'].close();
                updateDisplay(platform);
            }else
                popupWins['new_entry'].error('Platform "{0}" could not be created.');
        }

[monkey][snake] <.
 
Interesting idea monksnake I'll give that a shot.
 
Ok - first in reference to 1DMF: over zealous security? I suppose, but that wouldn't explain why my test case (which didn't use AJAX, but emulated several close cases I was trying to use) did work - but the AJAX one didn't.

monksnake: I actually moved the line you mentioned to just above the window.focus() line - thus only requiring that line to run when it is truly ready to close - and it worked!

Code:
	function addPlatform_cb(z){
		z = parseInt(z);
		if( z > 0 ){
			popupWins['new_entry'] = window.open(popupWins['new_entry'].location.href, 'new_entry', '');
			window.focus();
			popupWins['new_entry'].close();
			updateDisplay(platform);
		}else
			popupWins['new_entry'].error('Platform "{0}" could not be created.');
	}

I can see why (now) that, that line would cause it to work again - but I don't know why it wouldn't work in the first place.

Oh well, it is working now - thanks monksnake.
 
hmm so does FF have a problem maintaining relationship between parent and child windows?

good job MS.

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top