locked
Acessing values from cookies and Session RRS feed

  • Question

  • User-147238865 posted

    Can we access  value of cookies and session object using java script ?

    If yes then explain me how ?

    Saturday, December 20, 2014 4:02 AM

Answers

  • User-744022866 posted

    Javascript can access the cookie values, not the session object. But if your application needs session data to be shared to client side script, you can expose it to the page..using hidden fields or javascript variables..

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, December 20, 2014 5:12 AM
  • User541108374 posted

    Hi,

    Can we access  value of cookies and session object using java script ?

    • Cookies: yes. Cookies are being transferred to the browser where you execute your javascript. As such these can be manipulated in the browser by javascript
    • Session: no. Session state is meant to be kept on the server and usually holds information you don't want to expose to the browser.

    If you want to read out session state then you first need to make the decision as to why you want to do so. Session state is meant to be kept on the server for a reason. If you're sure you can/want to expose that information then don't use session state but simply display (or in a hidden way) in the html that you send to the browser. If you do want to manipulate them but don't want to expose them provide an endpoint like Web API or WCF that you call via ajax to manipulate it on the server. 

    For the cookies part with examples read this article: http://www.quirksmode.org/js/cookies.html

    Grz, Kris.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, December 20, 2014 5:24 AM

All replies

  • User-744022866 posted

    Javascript can access the cookie values, not the session object. But if your application needs session data to be shared to client side script, you can expose it to the page..using hidden fields or javascript variables..

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, December 20, 2014 5:12 AM
  • User541108374 posted

    Hi,

    Can we access  value of cookies and session object using java script ?

    • Cookies: yes. Cookies are being transferred to the browser where you execute your javascript. As such these can be manipulated in the browser by javascript
    • Session: no. Session state is meant to be kept on the server and usually holds information you don't want to expose to the browser.

    If you want to read out session state then you first need to make the decision as to why you want to do so. Session state is meant to be kept on the server for a reason. If you're sure you can/want to expose that information then don't use session state but simply display (or in a hidden way) in the html that you send to the browser. If you do want to manipulate them but don't want to expose them provide an endpoint like Web API or WCF that you call via ajax to manipulate it on the server. 

    For the cookies part with examples read this article: http://www.quirksmode.org/js/cookies.html

    Grz, Kris.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, December 20, 2014 5:24 AM