Refresh Parent page on close of pop up window - javascript

I have a Parent page in which, I have added a search functionality.
like below
function FunClick(StrPriCaption) {
var StrPriHTML = "";
if (StrPriCaption == 'AdvSearch') {
//document.getElementById('TxtCondition').value = "AdvSearch";
//form1.submit();
var StrPriReturnValue = "";
window.open('FrmInwardHdrAdvanceSearch.aspx', null, 'height=370,width=630,top=0,left=0,resizable=yes,scrollbars=yes');
}
}
And it works perfectly. It opens a pop up window page for me to search for.
But now what I want is, IF I close the pop up,I want to refresh the parent page.
I tried with below code in Child page, but it didn't refreshed the parent page.
function CloseWindow() {
window.close();
window.opener.location.reload();
}
How can I do this using Javascript?

Method 1
<script>
function popup() {
var win = window.open("", "Page Title", "toolbar=no, location=no");
win.document.body.innerHTML = 'Close Me';
}
</script>
Open Me
It creates a popup with a link to close the window and refresh parent.
Demo: https://jsfiddle.net/eke4f72r/
Method 2
<script>
function popup() {
var win = window.open("", "Page Title", "toolbar=no, location=no");
var win_timer = setInterval(function() {
if(win.closed) {
window.location.reload();
clearInterval(win_timer);
}
}, 100);
}
</script>
Open Me
It detects from the parent window if child is closed. If true, it reloads the page.
Demo: https://jsfiddle.net/gv6nmdn9/
EDIT When using method 1, make your parent to open the popup you want and just add this in your child:
Close Me

Detect it in parent page:
iframe=window.open("");
iframe.onclose=function(){
window.location="redirect";
};

Related

How to open new browser window in patient context by closing the existing open one in JavaScript

I have written JavaScript code which opens the application in new window.
But the problem is, when we switch patient from A to B it opens another window. So, currently, there will be 2 windows, one for patient A and another for B which is a clinical risk. It should close A and then open B.
What I want is that, when we switch patient, it should close the existing open window browser and open a new window for another patient.
<div id="openWrapper">
<div id="openEdge"></div>
</div>
$(document).ready(function () {
var params = [
'height=' + screen.height,
'width=' + screen.width,
'scrollbars=yes',
'resizable=yes',
'location=no'
].join(',');
var popUp;
function showModal() {
if (popUp)
popUp.close();
popUp = window.open('#Url', "_blank", params);
popUp.moveTo(0, 0);
popUp.focus();
LoadModal();
}
function LoadModal() {
var bcgDiv = document.getElementById("openEdge");
bcgDiv.style.display = "block";
}
function OnUnload() {
if (false == popUp.closed) {
popUp.close();
}
}
window.onunload = OnUnload;
showModal();
});

show one popup window at a time

how to show only one popup at a time?
i have created one page called parent.html. On parent page, i have two links one is register and another is login.
if i click register then register page open as child window
but
if i click login then already opened child box(register) should close and login child window should open or all previously opened child box should disappear.
my code is as below
<input type="button" value="login" onclick="ShowPopup('login.asp')" />
<script type="text/javascript">
var popup;
function ShowPopup(url) {
popup = window.open(url, "self", "toolbar=no,scrollbars=no,location=no,statusbar=no,menubar=no,resizable=0,width=100,height=100,left = 490,top = 262");
popup.focus();
}
</script>
</form>
<input type="button" value="register" onclick="ShowPopup('register.asp')" />
<script type="text/javascript">
var popup;
function ShowPopup(url) {
popup = window.open(url, "self", "toolbar=no,scrollbars=no,location=no,statusbar=no,menubar=no,resizable=0,width=100,height=100,left = 490,top = 262");
popup.focus();
}
</script>
</form>
i replaced "popup" with "self". now only one child window open at a time instead of multi and working perfect.
but when i close child window, parent page is not refreshing.
child window code as below
<form id="form1" runat="server">
<input type="button" value="Refresh Parent" onclick="RefreshParent()" />
<script type="text/javascript">
function RefreshParent() {
if (window.opener != null && !window.opener.closed) {
window.opener.location.reload();
}
}
window.onbeforeunload = RefreshParent;
</script>
</form>
please help me out
First thing , there is no need to define variable & function again .. and now coming to solution as --
<script type="text/javascript">
var popup;
function ShowPopup(url) {
if(popup)
{
popup.close();
}
popup = window.open(url, "Popup", "toolbar=no,scrollbars=no,location=no,statusbar=no,
menubar=no,resizable=0,width=100,height=100,left = 490,top = 262");
popup.focus();
}
</script>
In Your Child Page ,Head Section (To refresh parent window on close)
<script>
window.onunload = refreshParent;
function refreshParent() {
if(window.opener)
window.opener.location.reload(true);
}
</script>
By default, the reload() method reloads the page from the cache, but you can force it to reload the page from the server by passing parameter to true.
-- OR --
You can also some code in parent while you open a child window and look up for its child window if its close refresh itself..
<script type="text/javascript">
var popup;
var lookupTimer=null;
function ShowPopup(url) {
if(popup)
{
popup.close();
}
popup = window.open(url, "Popup", "toolbar=no,scrollbars=no,location=no,
statusbar=no,menubar=no,resizable=0,width=100,height=100,left = 490,top = 262");
popup.focus();
lookupTimer = setInterval(function(){ loolUpChild(); }, 1000);
}
function loolUpChild()
{
if((popup.closed))
{
window.location.reload(true);
clearInterval(lookupTimer);
}
}
</script>
create a global variable and maintain your flag if previous popup is open then close first and open new
Like
if(popup){
popup.close();
popup=false;
}
after that open new
window.open(url, "Popup", "toolbar=no,scrollbars=no,location=no,statusbar=no,menubar=no,resizable=0,width=100,height=100,left = 490,top = 262");
popup = true;

popup gets closed automatically

I have to refresh my parent window once I am done with the changes in the popup window, for which I use :
<BODY onunload="opener.location.reload();">
But this closes my window on any event, like save, whereas I don't want to get it close. I want to close my window only by clicking window close or by clicking close button in my jsp with "Javascript".
For closing thru "Close" button.
JS :
function closeAndRefresh(){
opener.location.reload(true);
window.close();
}
Solved the problem
JS:
var myclose = false;
function ConfirmClose()
{
if (event.clientY < 0)
{
myclose=true;
}
}
function HandleOnClose()
{
if (myclose==true)
{
opener.location.href = "<%=request.getContextPath()%>parent_jsp_path";
window.close();
}
}
HTML:
<BODY onbeforeunload="ConfirmClose();" onunload="HandleOnClose();">

communicate from parent to popup tab using jQuery

I've seen a lot of posts on communicating from the popup back to parent using window.opener.$('#myDiv').
But once a popup is opened, how does the parent: target and control a div inside the popup?
I'm opening my popup using window.open(url, 'myPopup', 'width=50', 'height=50');
You can adress the parent from the popup like this:
window.opener.foo();
or, on a generic way:
window.opener.property
And, to make sure the parent is still alive:
if (window.opener && !window.opener.closed)
{
// do something
}
On the parent view, you might try:
function add_content(page, text)
{
var texts = page.document.getElementById("content");
texts.innerHTML += text;
}
function start()
{
var popup_page = window.open("popup.htm");
var str = document.getElementById("content").innerHTML;
alert(str);
add_content(popup_page, window.location+" : "+str);
// window.close(); // ?
}

Show message in parent page after closing popup child

I have this code in a child window (popup):
function confirmation() {
var answer = confirm("Close?")
if (answer) {
var popup = window.open('../index.php?accao=some');
popup.document.getElementById('boxid').style.visibility="visible";
} else {
return false;
}
}
And, in the parent page I have:
<div id="boxid" style="visibility: hidden">Success</div>
What I want is to show the #boxid when I click in the popup to close. Why doesn't this code work?
UPDATE: still not working.
var popup = window.parent;
popup.document.getElementById('boxid').style.visibility="visible";
You should use window.parent to refer to the parent page.
What you are doing will open a new window.

Categories