How to handle closing of a previously opened window - javascript

I have a form where the user fills out stuff. Towards the end they click on a link which opens up a new window to record a video. Once they are done recording they close the window.
How can I react to the closing of the window on the first window form? I may want to submit the form automatically or have an ajax request started to display info etc... If this isn't possible what do you suggest?
Edit:
Parent window:
function someAlert() {
alert("success");
}
Child window:
window.onunload =window.opener.someAlert();

If you opened it with window.open, you can call
window.opener.someJavaScriptFunctionInParent();
call it with window.onunload or right before window.close()

Related

JS window.close popup isn't working after refresh of parent

Currently i'm creating a simple html/php/js project for internal use and found a little problem i can't solve by my self :-(
In my parent window is a JS function which opens and closes a popup with:
function openit(){
boersenfenster = window.open('boerse.php','_blank','scrollbars=no');
}
function closeit(){
boersenfenster.close();
}
Now after a while the parent window refreshes to get fresh data of a csv file and after the page reload the closeit() function isn't working anymore. In my thoughts, the parent window can't remember the openend popup after the refresh but how can i fix this problem?
Yes, I believe the parent window has lost the the link, so you can either
use ajax to update your page, or close the child window like this
window.addEventListener('beforeunload',function(e){
boersenfenster.close();
};
and then reopen the child window after the page has reloaded

How can I open a pop-up window without it being blocked?

I haven't found a single answer able to tell me what's the right way to open a popup.
Times have changed, and popups have been mostly replaced with fancybox-like boxes. However, there are still times when popups are needed.
For those cases, I don't want my popup to be blocked by the browsers.
What's the right way to open a popup without it being blocked? Even if it opens a new tab in the browser. I just want my popup to be open, and have control of it from the parent or vice versa.
Popup blockers will block any popup, unless it is opened because of an user action.
If the user clicks on a link, and a popup is opened in the click listener of that link, the popup blocker knows the user want to open something and will not (or should not) block the popup.
What you cannot do:
open a popup when the page is opened or closed
open a popup after a certain interval
open a popup after something asynchronous happens
What you can do:
open a popup in the on click listener
using target="_blank" in a anchor tag
You can access both windows with JavaScript variables:
if you use window.open, the parent can have a reference to the popup by assigning the result of window.open to a variable. Check out this article at W3Schools.
If the popup needs to have access to the window who has opened it, you can use window.opener. Check out this question.
try this, it works for me
$('#myButton').click(function () {
var redirectWindow = window.open('http://google.com', '_blank');
redirectWindow.location;
});
Js fiddle for this is here https://jsfiddle.net/safeeronline/70kdacL4/2/
if you want to open new tab after ajax call see this fiddle http://jsfiddle.net/safeeronline/70kdacL4/1/

How to close all the child pages on LogOut

We have a Master page and some child pages.One in them is popup by window.open function.The problem occurs When logout is clicked at Master Page.whole System is redirect to Login page except that Popup window.when i clicked on that child-page then it will redirected to Login Page.We need it be closed directly after logout is pressed.
Is there any script to close window if the window's location is known.I want to close that child page in Logout click.
like window.close('Authentication.aspx');
please tell me if it is possible
Thanks ,
Rakesh.
If you've opened a popup window using the following code, with a presumed name of "popupwindow"...
window.open("myurl.html", "popupwindow", "height, etc");
Then in your login page try the following javascript
if(window.name=="popupwindow"){
window.close();
}
UPDATE based on comments by OP...
If you no longer have a reference to the window (because the parent window has refreshed for example), then I believe it is almost impossible to detect whether a popup window with a particular name exists or not.
I say "almost impossible" because one option in this situation is to try opening the window again giving a blank URL. If the window already exists, then the window will remain on the same page as before, but now you have a reference to it, and can close as necessary.
var myWin = window.open("","popupwindow");
myWin.close();
However, the downside to this is that if the window does not exist, the user will see a blank window open before then being closed - so not a nice user-experience.

how to redirect a page which creates a popup, from that created popup using jQuery?

I am new to jQuery. I have a button on main page of my web application which creates a login pop. If user enters correct password then popup should be disappeared and the page which create that popup should go to welcome page of that user. I know how to disappear the popup if user enters correct password. How can I tell the explorer window which created that popup to go to the welcome page? Thanks in advance.
I believe with Javascript you can redirect the parent window with:
window.opener.location.href = <your url>;
And then simply close your popup:
window.close();
You don't need jQuery for this. Some simple JavaScript should do the trick.
//reload the parent window
//window.opener.location.reload();
//to change the parent window location
window.opener.location.href = "welcome.htm";
//close the current window
self.close();

Html javascript to open new window and close current window

I have a popup window and in that page I have the following code in the body.
<img src="...something"/>
The purpose is to have this popup window close when a user clicks on the image link, and to open a new page and be directed to http://www.example.com.
It works in IE and Chrome, but not in Firefox. The popup window closes but no new window is opened.
Any ideas?
Yes, I can repro this - interesting. setTimeout works around it:
onClick="javascript: setTimeout(window.close, 10);"
I can only guess that once the window closes (which happens before the hyperlink is followed) Firefox stops processing that page.
Edit: better make it 10ms delay - with 1ms Chrome doesn't close the window.
The question is actually solved for the opener but it didn't help my issue (not wished: the new windows under firefox keep the same size as the current popup).
So I find following solution:
function whenClicked()
{
window.close();
opener.location.href = "http://www.example.com";
}
or this if the ppage should open in a new tab:
function whenClicked()
{
window.close();
opener.open(http://www.example.com, '_blank');
}
When you add some functionality to an element's click event via javascript, that functionality is executed before the default click event (in this case, opening a new page), in order to allow for the possibility of intercepting and overriding the default event. The default behavior will only execute when and if the event returns a boolean value of true.
In this case, the additional functionality would be to close the window and my guess is that Firefox chooses to interpret this as "we're all done here", so the click event never returns true, and thus the new page never gets opened.
Evgeny's suggestion of using a short timeout would allow the click event to return true before the window is closed, thus allowing the new window to open.

Categories