To Close Browser Window in servlet

Discussions

Web tier: servlets, JSP, Web frameworks: To Close Browser Window in servlet

  1. I have a down load servlet in which file download is performed by the following lines od code

     response.setContentType("application/octet-stream");

     response.setHeader("Content-Disposition", "attachment;filename=" + name +";");

    then i say

    PrintWriter out = response.getWriter();
            out.println ("<script>");
            out.println ("window.close()");
            out.println ("</script>");

    .But Window is not closes.Could any one tell me what the problem might be!.and is there any way to close browser window.
    thanks
  2. It will be better if you try the following constuction:

    <BODY OnLoad="javascript:parent.window.close()">
  3. Dear,

    Calling Javascript like this is not recommended. You need to trigger an event to call the java script. If you are using some event to close the window then call a function on that event and call this method ( window.close) in that function. It will surely work ..

    Just like

    onClick='close_wind();'

    function close_wind()
    {
      // all event operations
      window.close ;
    }

    Regards,

    Shahzad