2

I am trying to set session value in javascript :

//session value 5400
//remainingSeconds value 5399
@Session["remain-seconds"] = remainingSeconds;

But i am getting the following error:

invalid left-hand side in assignment

Is it possible to set session variable inside javascript?

2
  • 2
    Sessions cannot be accessed by JavaScript Commented Feb 1, 2016 at 20:25
  • @jasonwarford Are you sure ? Commented Feb 1, 2016 at 20:26

1 Answer 1

2

No, session variables are server side. You'd have to have a little ajax call to talk to the server to set it. There are plenty of examples of this via google searches. You can see one example here.

For a one-time evaluation (on the server, before it hits the client), you can set it (sort of) in the code section of your script, but I think that's different from what you're asking to do. See this thread.

Sign up to request clarification or add additional context in comments.

3 Comments

I am using setinterval to implement countdown timer. It would be heavy process to send ajax request in every second. I did it because user can change javascript variable from console and i need unchangable global variable in javascript
There's options for that sort of thing. Session isn't one of them. =) Please open a new question for recommendations on ways to solve your underlying problem.
Okey i found solution, simple wrap inside (function(){ : )))

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.