Refresh parent window when the pop-up window is closed - javascript

Is there any way I can refresh the parent window when a popup window is closed without adding any javascript code to the popup window?
I have a page parent.php on which users can click "open popup" to open a popup window. This popup window shows some flash content and its not possible for me to add something like
window.onunload = function(){
window.opener.location.reload();
};
to the popup window page markup.
Is there any other method to achieve this?
Thanks

To make this work in all major browsers, you need to handle the unload event handler in the pop-up and do the reloading in the main window. In the main window, add
function popUpClosed() {
window.location.reload();
}
In the pop-up:
window.onunload = function() {
if (window.opener && !window.opener.closed) {
window.opener.popUpClosed();
}
};
So the answer to your question is generally no, if you need your code to work in all browsers, in particular IE.

I'm sure you can just add this to parent.php:
var myPop = "pop up window selector"
myPop.onunload = function(){
location.reload();
};

The problem with Tim Down's method is that it doesn't answer the original question. The requirement is that you cannot add any code to the pop-up window.
One solution that I've found, while not particularly elegant, is effective across all browsers I've tested on.
You will be simply polling the newly created window object continuously, checking if it's still open.
On parent window:
var register;
var poll;
function isOpen(){
if(register.closed){alert("Closed!"); clearInterval(poll);}
}
function create(){
register = window.open("http://www.google.com","register","width=425,height=550");
poll=setInterval("isOpen()",100); //Poll every 100 ms.
}

had similar problem to detect the closing popup in the parent window. I think the reason was
in not setting the document.domain property.
any way add to Tim Down answer the document.domain property for both window and popup like this:
<script type="text/javascript">
document.domain='<?=$_SERVER['SERVER_NAME']?>';
</script>
and instead of
window.onunload = function() {
if (window.opener && !window.opener.closed) {
window.opener.popUpClosed();
}
};
I used in the popup :
<body onunload="window.opener.popUpClosed();">

Related

How do I close the current browser tab on ReactJS?

Basically, I want the current browser tab to be closed on a click of a button. How do I implement this in ReactJS? Tried window.close() but did not work.
For security purposes, JavaScript can't close a window that it didn't directly open.
https://developer.mozilla.org/en-US/docs/Web/API/Window/close
As you can see from the example, the originating script (which opened the window) must also be the script that closes the window. A new window isn't capable of closing itself via JavaScript.
//Global var to store a reference to the opened window
var openedWindow;
function openWindow() {
openedWindow = window.open('moreinfo.htm');
}
function closeOpenedWindow() {
openedWindow.close();
}
On your click event handler, try this :
window.open("", "_self");
window.close();

How to open a new window in the same page

I have a button:
<button>Click</button>
I want click it and open a new window in the same page, I bind the click event an event handle:
$('button').click(function () {
var newurl="http://" + window.location["host"] + ";
window.open(newurl, '_parent');
})
But is always open in new tab or new window, the second parameter I have try _self _top.
I even have try window.location.href(newurl)
So how can I solve this problem?Does it matter with browser or OS? I view it in Mac OS's firefox and chrome.
Well that's not possible technically, it also depends on browser tab settings, window settings and third party tab manipulation plugins or addons.
Update
OP has cleared the confusion of all through his comment below his question, this is what you need to set a new url for current window:
$('button').click(function () {
var newurl="http://" + window.location["host"];
window.location.href = newurl; // or simply window.location
})
window.location =newurl; will open a new window replacing the parent
Have you tried this?
<script type="text/javascript">
window.open ('test.htm','_self',false)
</script>
If you mean you want to replace the current page, window.location = newLocation; will do.
If you want a modal popup, try JQuery UI Dialog.

How to control opener page which is opened by window.open method

I have a page in which i m calling another popup by window.open method. The only thing how can i change a label in opener page from popup page while the popup page is still alive ie which is not closed yet
It's better to let the opener window take care of changing values by exposing a small API to the popup window.
I've outlined it here: javascript - pass selected value from popup window to parent window input box
It should be like this:
window.opener.document.getElementById('label1').value = "the new value";
<script>
function myFunction() {
var additionalWindow = window.open("/additional");
// Write on the additional window
additionalWindow.document.write('written from separate window');
// Call a function on the additional window
additionalWindow.someFunction();
}
</script>
Here's Mozilla's documentation on window.open().

retain popup window when navigating website

The following code produces a javascript popup when a page is loaded and then closes the popup window when the parent window is closed.
<script language="JavaScript">
var player;
function load() {
player = window.open('http://www.google.com','player','height=320,width=320,scrollbars,resizable');
}
function unload() {
player.close();
}
window.onload = load;
window.onunload = unload;
</script>
This is working fine, however it closes the popup window when switching pages on the site. Is there any way this can be altered to keep the popup window until the actual URL/Site window is closed?
I think you have to check the status of the parent window from the child, instead of vice-versa.
This code has not been tested, but I'm taking a stab at the concept:
window.opener.onunload =
setTimeout(
function(){
if (!window.opener) self.close();
}, 1000);
Short answer is NO, the only way to hook into the event that closes the window is onunload, which fires when the document is unloaded (like when you navigate to another page).
Maybe you could check window.opener on the popup (with a timer, or onfocus), to check if the window that originated the popup still exists, then close the popup if it doesn't.
This may help you:
<script language="Javascript">
var player;
function load() {
player = window.open('http://www.google.com','player','height=320,width=320,scrollbars,resizable');
}
function unload() {
if (!(window.location.equals('http://yourSiteName.com'))) {
player.close();
}
}
window.onload = load();
window.onunload = unload();
</script>

javascript-how to open window (from hyperlink) and then close it with delay of 5 sec?

I am trying to open new window from hyperlink using java script and then auto close it in five seconds. It either closes right away or doesn't close at all. Here are some samples of code I was using:
"function closeOnLoad(myLink){var newWindow=window.open(myLink);newWindow.onload=SetTimeout(newWindow.close(),5000);}" + LinkText + ""
You're better off closing the window from the parent instead of defining an onload handler within the child. Due to security restrictions, you simply may not have access to modify child window events.
function closeOnLoad(myLink)
{
var newWindow = window.open(myLink);
setTimeout(
function()
{
newWindow.close();
},
5000
);
};
}
you need to use what is called a 'closure' to wrap the timeout in. It's like the function to timeout and then close is wrapped within another function.
I won't go into detail here, but lookup javascript and closures and play around to see how they work.
Here's a link to help get you started: http://www.jibbering.com/faq/faq_notes/closures.html
The window closing code should be in the window's code:
$(document).ready(function() {
setTimeout(function() {
window.close();
},5000);
})
BUT, you will get a popup asking for the user to confirm if you try & close the popup that way.
To unload is the unload() function. Here you have an example.

Categories