SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Aug 2010
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy how to run a javascript if the web page window is closed

    Hi guys,

    I want to run a javascript function when the browser window is closed.i'm developing jsp application.i want execute a update database when the user closes the window.i think i can do this by putting update query in a javascript function and executing this query when the window is closed.

    If any of you have any other apporach then pls let me know.

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,807
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Since JavaScript runs in the web browser window it must run before the window is closed. Once the window closes the JavaScript is gone.

    The only way to have the server detect when the person leaves the web page (whether they close the browser window or simply open a page from somewhere else is to use an ajax script that sends an "I.m still open" message to the server at regular intervals. Once a long enough time has passed since the last such signal was received then the server knows that the JavaScript is no longer running - in most cases because the person left the web page.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  3. #3
    SitePoint Member
    Join Date
    Aug 2010
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thank you.


    May you pls tell me how to use the ajax code for doing that.may you pls pass me links that has code for doing this.

    Thank you once again.

  4. #4
    SitePoint Member
    Join Date
    Aug 2010
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kotaravitez View Post
    May you pls tell me how to use the ajax code for doing that.may you pls pass me links that has code for doing this.
    Have you asked Google? Google knows a lot. I doubt you'll find a copy-and-paste solution, because your code needs to talk to your server. At any rate, if you Google it, you should be able to find what you need. Google is your friend.


Bookmarks

Posting Permissions

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