Carsten Løvbo Andersen Carsten Løvbo Andersen - 4 years ago 451
Javascript Question

JavaScript session?

How do you make a JavaScript session?

if (gameWin) {
Session["BrugerTid"] = document.all("counter").innerHTML;
window.location = "Won.aspx";

The above code doesn't work, does anyone have an idea what could make it work?

Here is the JavaScript that makes the "counter":

var timeLeft = 120;
function decrementCounter() {
if (timeLeft > 0) {
document.all('counter').innerHTML = "" + timeLeft + "";
setTimeout("decrementCounter()", 1000);
document.getElementById("start_button").style.display = 'none';
document.getElementById("blackout").style.display = 'none';
else {
window.location = "Failed.aspx";

The counter is working and it starts at 120 and goes down until it hits 0. If we complete the puzzle before hitting 0 then we win. We would like to make a section with the time left and send it to our Won.aspx site.

Answer Source

You cannot access the Session object (server-side) directly from javascript (client-side), but maybe you could send the time to your .aspx page as a parameter instead?

Like this in javascript:

if (gameWin) {
    window.location = "Won.aspx?BrugerTid=" + document.all("counter").innerHTML;

And in Win.aspx:

protected void Page_Load(object sender, EventArgs e)
    Session["BrugerTid"] = Request.QueryString["BrugerTid"];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download