• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 641
  • Last Modified:

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

0
mcgants
Asked:
mcgants
2 Solutions
 
orbulatCommented:
0
 
mangesh004Commented:
Hi,
     Why didnt you go for Modal Window (showModalDialog). If you are using modal window then focus will be on the Calendar window and it will not alllow the user to close the parent window until he chooses the date.

http://www.codestore.net/store.nsf/unid/DOMM-4R5MDQ?OpenDocument

Read this link for better understanding and sample code

Regards,
Mangesh
0
 
mcgantsAuthor Commented:
Thanks both, appreciated :D
0
 
bsdotnetCommented:
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();">
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now