Calling a web page before current page closes

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bakpao
    New Member
    • Aug 2007
    • 21

    Calling a web page before current page closes

    I want to perform the following: when a page is unloaded (e.g. user opens a different page), it calls to the server and returns some information (e.g. how long the user had the page open for). To do that, I had something like this:

    [code=javascript]
    <iframe id="myframe" src="" style="display: none"/>

    <script type="text/javascript">
    this.onbeforeun load = function()
    {
    document.getEle mentById("myfra me").src = "savestate.aspx ?data=123";
    // alert ('Hi');
    }
    </script>
    [/code]

    The code above does not work since opening the page on the frame takes time and since it is too slow since the page is unloaded before that link is called. If I uncomment the alert statement, the script works. However I do not want the user to see the message box so I cannot do that. I have tried setTimeOut but it does not work since the page is being unloaded.

    Anyone has any idea on how to solve this problem? Thanks.
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    The page will be unloaded immediately. The only way to prevent this is to use onbeforeunload (on browsers which support it) and popping up an alert. If you return a string, it will be displayed in a confirmation alert.

    Comment

    • bakpao
      New Member
      • Aug 2007
      • 21

      #3
      Thanks for that, I will try to find a different way.

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        No problem. Good luck in your project.

        Comment

        Working...