Carsten Løvbo Andersen Carsten Løvbo Andersen - 2 months ago 8
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 + "";
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

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"];
}