Close a popup window when browser closes?

All,

How would I close a popup window when the brower closes? The popup window itself will update a textbox in the main browser when selected.

Basically, it's a calendar popup that is working absolutely fine but I need the popup to close when the browser window closes. How can I get it to do that? window.showmodaldialog? (I tried but it's not updating the field on the browser like it used to.

Is there any way to do that?

LVL 1
davismAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
the_billConnect With a Mentor Commented:
You can add this to the body

onunload="javascript: killit()"

then in the head section, add this script

<SCRIPT language="JavaScript1.2">
var my_window;
function exitpop()
{
my_window= window.open ("",
  "mywindow1","status=1,width=350,height=150");
my_window.document.write('<H1>Calender!</H1>');  
}
function killit(){
if (my_window)
my_window.close();
}
</SCRIPT>

Note: you need to name your calendar popup window, which you then access to close.
0
 
the_billCommented:
*** Adding to my previous post, the same way we wrote to the popup window, you can get the data when the popup is closing
0
 
davismAuthor Commented:
Maybe I am a little confused as I have:

function EditValue(controlID)
{
   var strPassedParam = controlID;
   intCheck = strPassedParam.indexOf("%");
   if (intCheck >= 0)
   {
      strPassedParam = strPassedParam.replace(/<\%=\s*|.ClientID %>/g,"");
      //This addition is due to master page.
      strPassedParam = "ctl00_cphBody_" + strPassedParam;
   }
   return strPassedParam;
}

function isNumeric(value)
{
  if (value == null || !value.toString().match(/^[-]?\d*\.?\d+$/)) return false;
  return true;
}

function SetControlValue(controlID, newDate, isPostBack)
{
    popUp.close();
    document.getElementById(EditValue(controlID)).value=newDate;
    //__doPostBack(controlID,'');
}

var popUp;
function OpenPopupPage (pageUrl, controlID, isPostBack)
{
    popUp=window.open(pageUrl+'?Date='+document.getElementById(EditValue(controlID)).value + '&controlID='+controlID+'&isPostBack='+ isPostBack,'popupcal', 'width=250,height=300,left=200,top=250');
}

And in the code-behind for the calendar (i.e. PageURL) I have it returning back the selected date.

Is I change the above function "OpenPopupPage" to do:

popUp=window.showmodaldiaglog(pageUrl+'?Date='+document.getElementById(EditValue(controlID)).value + '&controlID='+controlID+'&isPostBack='+ isPostBack,'popupcal', 'width=250,height=300,left=200,top=250');

It will open the page but a little ugly and it will not post the date selected to the parent page textbox.

But it I put it back to the "window.open" everything works great albeit then I close the browser window the calendar popup window still remains.


0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
davismAuthor Commented:
My bad... forgot to add this:

on the code-behind of the page from the pageURL which is the popup..I have this:

  void Page_LoadComplete(object sender, System.EventArgs e) {
    OKButton.OnClientClick = "javascript:window.opener.SetControlValue('" + this.controlToEdit + "','" + lblDate.Text + "','" + this.isPostBack + "');";
  }

the lblDate.Text is the date selected.
0
 
the_billCommented:
Do you want the popup to do the same thing the OK button does when its clicked automatically when its closed? ie closing the popup should click the OK button so your parent is updated?

in that case, in the popup window, add this to the body tag

<body onunload="javascript:document.getElementById("<%= OKButton.ClientID%>").click()">

that will simulate the click event.
0
 
davismAuthor Commented:
the_bill,

Maybe I'm missing something but that wouldn't address the aspect of the closure if the brower window closes. The window.showmodaldialog put the focus on the popup and nothing can be done on the brower until/unless the popup is closed. However, the showmodaldialog doesn't function the same.

With the information I provided how could I have the same fundamental functionality as the showmodaldialog in the popup I have?
0
 
davismAuthor Commented:
the_bill,

this seems to be working when I put this in the main browser page (not in the master) but in the page. It's not the same thing as what you provided but somewhat similiar:

window.onunload = ClosePopUp;
function ClosePopUp()
{
   popUp.close();
}

Do you see any potential issues?
0
 
davismAuthor Commented:
the_Bill,

Although this didn't cover the exact situation it did reference some aspects that needed to be done (i.e. close() ) and due to that I am closing the ticket and awarding you the points.

Thanks much for the informaton. Made me think a little which obviously helped coming up with the appropriate solution.

Thanks much and much appreciated.
0
All Courses

From novice to tech pro — start learning today.