title: close browser window using asp.net
body: <p>I have implemented cron job for sending mail to customer in Windows 7. I would like to close the browser window on server after specific period of time.</p>
  
  <p>The below HTML file contains Javascript in which I have used <code>window.close()</code>. This function is used to close the window but it only refreshes the browser page and does not close the window.</p>
  
  <pre><code>&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head runat="server"&gt;
      &lt;title&gt;Open/Close Window&lt;/title&gt;
      &lt;script type="text/javascript"&gt;
  
          function CloseWindow() {
              window.close();
          }
      &lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;form id="form1" runat="server"&gt;
      &lt;div&gt;
          &lt;asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" /&gt;
          &lt;%--&lt;input type="button" value="Open 'myWindow'" onclick="openWin()"&gt;
      &lt;input type="button" value="Close 'myWindow'" onclick="closeWin()"&gt;--%&gt;
      &lt;/div&gt;
      &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>I have called the Javascript function on page load event of <code>.cs</code> file. The below code refreshes the page but does not close the window.</p>
  
  <pre><code>protected void Page_Load(object sender, EventArgs e)
  {        
      Page.ClientScript.RegisterOnSubmitStatement(typeof(Page), "closePage", "window.onunload = CloseWindow();");
  }
  </code></pre>