Friday, 9 March 2012

prompt a user at the time he leaves the current page using javasript.

Sometimes it happens that we have to prompt and ask the user that he is really willing to leave the current page or not. In my example, I have a survey on my site. when the user is in the progress of survey,suddenly he clicks on a link,that causes him to leave the page. At this time, I have to store its survey response to my database. Here is a simple javascript event to handle this situation.

  window.onbeforeunload = function () {
        return doSubmit(); //here doSubmit is my ajax method,that makes an ajax call and save the data.
    }

Or sometimes we have to clear the browser data when user leaves the page. This is a simple block of javascript.

Have fun.

No comments:

Post a Comment