By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,654 Members | 1,580 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,654 IT Pros & Developers. It's quick & easy.

Calling a web page before current page closes

P: 21
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:

Expand|Select|Wrap|Line Numbers
  1. <iframe id="myframe" src="" style="display:none"/>
  2.  
  3. <script type="text/javascript">
  4. this.onbeforeunload = function()
  5. {
  6.     document.getElementById("myframe").src = "savestate.aspx?data=123";
  7.     // alert ('Hi');
  8. }    
  9. </script>
  10.  
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.
Oct 30 '07 #1
Share this Question
Share on Google+
3 Replies


acoder
Expert Mod 15k+
P: 16,027
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.
Oct 31 '07 #2

P: 21
Thanks for that, I will try to find a different way.
Oct 31 '07 #3

acoder
Expert Mod 15k+
P: 16,027
No problem. Good luck in your project.
Nov 1 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.