I am calling the parent page javascript function from child window with code below, which displays some alert message on parent page and closes the child window.
window.opener.CallAlert("test is"+test);
opener.focus();
self.close();
What is happening with above code snippet is alert comes on parent window but focus remains on child window. i need explicitly go to parent window. Once I click ok on alert child window closes.
Everything is fine but I want the to focus on parent window once i call the window.opener.CallAlert() from child window.
Edit
Looks like opener.focus(); works on IE but not mozilla. Any workaround for firefox?
It would appear that this is not guaranteed to work and is at the discretion of the browser.
See this thread: window.opener.focus() doesn't work
just try the obvious:
window.opener.focus();
try with delay setTimeout(function(){self.close()},300) and then display alert on parent page. It will schedule to close the current window after some delay automatically with display of alert message on parent signal
Related
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
I want to refresh the parent window after closing the pop up window.
I have used
window.opener.location.reload();
This works fine in one case when the parent window is the calling window. But there is another scenario where we set the parent of the calling window as another window.
So instead of loading the calling parent window it loads the other page. Can we set a fixed parent window as in my case its obviously changing due to some business rules.
Here is the scenario:
I'm in parent window. I click on a link, which opens a child window
Now I'm in child window, I enter some data and click on a submit button
Now, the child window refreshes. After the child window completes refreshing, the parent window refreshes.
After the parent window refreshes, the child window goes background, which I don't want. I want that to remain foreground.
NOW, I searched how to get the reference of child window from the parent window and found this link-
http://www.codeproject.com/Articles/25388/Accessing-parent-window-from-child-window-or-vice#b
My question is - Will I be able to successfully get the reference of the child window even after the parent window has refreshed?
More info: This happens only in IE and not in Chrome/Firefox.
UPDATE: As per this link - https://www.daniweb.com/programming/web-development/threads/48485/parentchild-windows-references
The parent will not have control over child anymore. But is there a way I can maintain the focus on the child window even after the parent window refreshes?
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.
Anyone knows the difference?
The problem I met is that the page stops working correctly when opened by window.showModalDialog
window.showModalDialog vs window.open
Window.open will open up a new window through Javascript, with the URL and other features of the window that u pass as parameters. Here the parent window which opens the new window and the child window are independent windows.
Eg. Below
`window.open('winOpen.htm','name','height=255,width=250,toolbar=no,directories=no,status=no,
linemenubar=no,scrollbars=no,resizable=no');`
Window.showModalDialogue again works smilar to a window.open only diffrence being its a Modal window, It opens up as a new window but doesnt allow the user to access the parent window, unless you explicitly close it.
Here the child window is dependent on the parent window. If you close the parent window the child would also get closed.
window.showModalDialog("xpopupex.htm","name","dialogWidth:255px;dialogHeight:250px");
ShowModalDialogue windows can be used when u want the user to perform a particular action in the new window before he access the parent window again. like login before he can access the parent page..
tryed to make it as simple as possible...hope this help.. ;)