Link to home
Start Free TrialLog in
Avatar of mcgants
mcgants

asked on

Close javascript window in code

Hello,
I have written a script in my application that opens a popup calendar as follows:

  Dim myScript As String = "<script language='JavaScript'>window.open('Calendar.aspx?ReturnTo=" & txtStartDate.ID & "', 'cal','width=180,height=160,left=270,top=180');</script>"

  RegisterStartupScript("popupCal", myScript)

This part works fine, and the window closes when the user clicks a date. The problem I am having is that I want to close the window when the user exits the page. I have tried using a function linked to onBodyUnload and a direct script (below) but neither work - failing as a Javascript error stating that the object 'cal' is undefined.

  Dim myScript As String = "<script language='JavaScript'>cal.close();</script>"

  RegisterStartupScript("closeCal", myScript)

Any help with my existing solution or an alternative would be greatly appreciated.
Cheers,
mcg

Avatar of orbulat
orbulat
Flag of Hong Kong image

ASKER CERTIFIED SOLUTION
Avatar of orbulat
orbulat
Flag of Hong Kong image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mcgants
mcgants

ASKER

Thanks both, appreciated :D
Dim myScript As String = "<script language='JavaScript'>newwindow = window.open('Calendar.aspx?ReturnTo=" & txtStartDate.ID & "', 'cal','width=180,height=160,left=270,top=180');</script>"
  RegisterStartupScript("popupCal", myScript)


' add this in your opener window's body tag
<body onunload="if(false == newwindow.closed) newwindow.close();">