Results 1 to 9 of 9

Thread: close current browser window

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2003
    Posts
    22

    close current browser window

    Hi,

    How can I close the current browser window after user click the button?

    I know Javascript "window.close" can do so, but is it possible to do it in ASP .NET?

    Regards,
    Calvin

  2. #2
    Frenzied Member Fishcake's Avatar
    Join Date
    Feb 2001
    Location
    Derby, UK
    Posts
    1,092
    You'll have to use Javascript as server side technology can't close a client-side browser.

  3. #3
    Hyperactive Member greg_quinn's Avatar
    Join Date
    Nov 2002
    Location
    South Africa
    Posts
    366
    Hi YellowCat,

    You can response.write some Javascript to the client browser...

    i.e

    Private Sub Closewindow(Sender as Object, E as EventArgs)
    Dim jScriptString as String

    jScriptString = "<SCR" + "IPT>"
    jScriptString += "window.close();"
    jScriptstring += "</SCR" + "IPT>"

    Response.Write(jScriptString)
    End Sub

  4. #4
    Frenzied Member Fishcake's Avatar
    Join Date
    Feb 2001
    Location
    Derby, UK
    Posts
    1,092
    There's loads of different ways you can action this but at the end of the day you've still got to use script.

  5. #5
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    Just remember, the browser will dialog to the user stating the web page is trying to close the browser, unless it was a window initially opened by javascript.

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Jul 2003
    Posts
    22
    now I use following code to close the browser window:

    Dim strScript As String
    strScript = "<script language='javascript'>window.close()/script>"
    Page.RegisterStartupScript("Script", strScript)

    Is it the best way to do that?

    Regards,
    Calvin

  7. #7
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    Well, depends on your view....

    I would say just throw an OnClick attribute in the actual aspx html to close the window...

    Or you could do it the way you suggested.

    My way means the server doesn't have to process the button click and register script at page render.

    Your way is a tad more secure and seperates user interface from code logic... in my way, a web-designer or malicious person can edit the html client-side. but then that all depends if javascript is even enabled.

    Really its up to you. Like I said, the user can always say 'No' to the IE dialog requesting the user to allow the web page to close the browser window (unless it was initally opened by javascript, in which case it simply closes).

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Jul 2003
    Posts
    22
    so, how can I close the browser window in 'OnClick' events? Could you mind to give me some hints?

  9. #9
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    Code:
    <input type="button" value="close" onClick="window.Close();">

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width