Close javascript window in code

Posted on 2006-05-03
Last Modified: 2007-12-19
I have written a script in my application that opens a popup calendar as follows:

  Dim myScript As String = "<script language='JavaScript'>'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.

Question by:mcgants
    LVL 10

    Expert Comment

    LVL 10

    Accepted Solution

    LVL 2

    Assisted Solution

         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.

    Read this link for better understanding and sample code

    LVL 6

    Author Comment

    Thanks both, appreciated :D
    LVL 12

    Expert Comment

    Dim myScript As String = "<script language='JavaScript'>newwindow ='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();">

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Join & Write a Comment

    Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
    This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

    732 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now