27

I am getting name and email id of a user after he logs in via facebook to my website.. I want to add those variables in session on login form itself using javascript; I tried following:

FB.api('/me', function(me) {
        if (me.name) {
            document.getElementById('auth-displayname').innerHTML = me.name; <% Session["fbName"] = me.name; %>
        }
}

it gives error like me (in this line: <%Session["fbName"] = me.name; %>) does not exist in the current context etc.. my div "auth-displayname" is getting that value but I'm having problem with session variable

How can I do this

fnkr
  • 7,080
  • 6
  • 41
  • 54
Sam
  • 421
  • 2
  • 5
  • 8

4 Answers4

52

You can use

sessionStorage.SessionName = "SessionData" ,

sessionStorage.getItem("SessionName") and

sessionStorage.setItem("SessionName","SessionData");

See the supported browsers on http://caniuse.com/namevalue-storage

User
  • 692
  • 5
  • 11
10

A session is stored server side, you can't modify it with JavaScript. Sessions may contain sensitive data.

You can modify cookies using document.cookie.

You can easily find many examples how to modify cookies.

Halcyon
  • 54,624
  • 10
  • 83
  • 122
  • Many languages keep session variables on the client side, via cookies. – Bernardo Siu Dec 03 '14 at 20:40
  • @bernando for example? – Yang Dec 04 '14 at 09:34
  • 1
    @BernardoSiu typically the client only sees a session identifier cookie, and even that can be hidden from JavaScript with _HttpOnly cookies_. It's important to realize the difference between sessions and cookies, they're not the same thing. – Halcyon Dec 04 '14 at 13:13
  • Here's some info on the session API https://developer.mozilla.org/en-US/docs/Web/API/Window/sessionStorage – Rimian Jan 11 '18 at 04:23
5

You could better use the localStorage of the web browser.

You can find a reference here

Thanassis_K
  • 272
  • 1
  • 9
  • I didn't downvote, but it could be because localStorage and session storage are fairly different things - BUT, depending on the need, you're right that it *could* be useful for solving the question-asker's original problem...whatever it is. – Katana314 Nov 19 '13 at 19:51
  • 1
    localStorage? use sessionStorage – User May 06 '14 at 14:56
  • chome has issues with localstorage – Mikeys4u Feb 22 '18 at 11:05
  • @Mikeys4u what issues? – tdelam Jun 15 '18 at 19:42
  • @Thanassis_K for the record, your question was probably down voted in the past because its a link only answer. Links can change or die and an answer that includes example code is always going to be more useful than a link only answer. – Ortund Mar 19 '19 at 08:03
3

It is very important to understand both sessionStorage and localStorage as they both have different uses:

From MDN:

All of your web storage data is contained within two object-like structures inside the browser: sessionStorage and localStorage. The first one persists data for as long as the browser is open (the data is lost when the browser is closed) and the second one persists data even after the browser is closed and then opened again.

sessionStorage - Saves data until the browser is closed, the data is deleted when the tab/browser is closed.

localStorage - Saves data "forever" even after the browser is closed BUT you shouldn't count on the data you store to be there later, the data might get deleted by the browser at any time because of pretty much anything, or deleted by the user, best practice would be to validate that the data is there first, and continue the rest if it is there. (or set it up again if its not there)

To understand more, read here: localStorage | sessionStorage

Art3mix
  • 1,098
  • 5
  • 15