?
Solved

How can I have a javascript message box show when the user's Session has timed out?

Posted on 2007-10-15
9
Medium Priority
?
601 Views
Last Modified: 2013-11-07
I have a website where a user can sign in and have rights to other web pages as an administrator.  If the browser is idle after 10 minutes, the users session timeout's out.

How can I have the web page reconize the session is expired and have a message box appear that warns them that there session has expired?

I haev seen this with bank web pages, where you log in, look at your bank account, but if you leave the computer for a certain amount of time, a message box asks you if you are still there to continue your session.  How can I achieve this?

I am writing the webpages in ASP .NET.

Thanks
0
Comment
Question by:mmedi005
  • 5
  • 4
9 Comments
 
LVL 9

Accepted Solution

by:
jmanGJHS97 earned 1500 total points
ID: 20080517
This appears to be similar to what you are doing.

http://www.experts-exchange.com/Web/Web_Languages/JavaScript/Q_21630889.html
0
 

Author Comment

by:mmedi005
ID: 20080774
Are they doing this with AJAX?   How can I do this without AJAX?

Does look like the same problem...I'm kind of mixed up on how he placed this code in this web site.  Can I put this in a master page, so that all can use these functions?  Also what if the user never pogged in, and is only accessing the public web pages, will the function always execute if the session variable does not exist?
0
 

Author Comment

by:mmedi005
ID: 20081102
I don't really understand the code....I see that it does sound like the same thing..but can you walk me thru to understand where the javascript is going.....

this works as a timer.....

// Set timout little less than actual timeout so as to give user time to react.
Timeout = 1 * 60 * 1000; // Milli-Seconds (for 20 minute timeout - warning in 15 minutes)

setTimeout("AlertUser();", Timeout);

function AlertUser()
{
    var ans = confirm("Timeout in 5 minutes. Reset?");

    if (ans)
    {
        var oImage = new Image;
        oImage.src = "AnyImageName.gif?" + Math.random(); // Added random URL so that image is not cached.
        setTimeout("AlertUser();", Timeout);
    }
}

btu why the image, and how does this restart the session timeout?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:mmedi005
ID: 20081338
Can this be done on the server side instead of using javascript?
 

Can anyone help me?????
0
 
LVL 9

Expert Comment

by:jmanGJHS97
ID: 20081624
This does not use AJAX at all.  It simply initializes a timeout to the number of seconds you want.  I believe they are using an image because they want to force the page to not cache.

You can certainly put this in a js include file if you want.  That may be helpful if you want more than one page to time out.  I'm not sure what session variable you are talking about.  There don't appear to be any session variables in this script.

The timeout starts over whenever the page is refreshed or re-rendered because the javascript is reinitialized.

You can probably do this server-side, but this is a JavaScript question, so I assumed you wanted to use JavaScript.  There's really no need to do it server-side, but it's possible.
0
 

Author Comment

by:mmedi005
ID: 20085742
Lets say my Session variable is a website with a shopping cart.  I have some items and I leave and see the message box show a warning that the session will expire.  

If I click OK and continue to check off check boxes, will the Session variable expire, if so, how can I get the session Timeout to restart when the user clicks ok?
0
 
LVL 9

Expert Comment

by:jmanGJHS97
ID: 20085848
JavaScript is not directly reading the session variable.  What it's doing is setting a countdown and alerting you when that is about to expire.

JavaScript is a client-side language, and as such, cannot set/edit/reset session variables directly.  You will have to use some server-side code to do that.  What your question title suggests is that you want a way to know if the session has timed out, which you can simulate using JavaScript.  If you want to modify session variables, this is not a JavaScript task.  You will need to ask a question under a server-side language topic.

Also, I strongly recommend not trying to store information in the session to begin with.  Doing so is usually considered lazy coding practice and unnecessary.  Submitting information in a form and passing values to and from functions is by far the preferred method.  You will find that the performance and stability of your application is much better if you avoid session variables for the most part.
0
 

Author Comment

by:mmedi005
ID: 20085959
I agree, but the Session variable is used for user name and password.  Really I stay away from Session variables.
0
 
LVL 9

Expert Comment

by:jmanGJHS97
ID: 20086238
ok, well that's good.  that doesn't change the fact that JavaScript can't alter session variables, so you are going to have to find a server-side solution.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses
Course of the Month16 days, 9 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question