locked
how to get session value in javascript RRS feed

  • Question

  • Hi every one

        i need to get some session varibale value in javascript on application page.

    how can i get value from session. give me way for this.

    Thanks


    Navaratan Sharma

    Wednesday, March 14, 2012 2:28 PM

Answers

  • Session is a server object. You cannot read values from session using javascript (there are security reasons for that too).

    You should get value from session on server and put it to javascript (do you generate js on server?).

    Also, you may use HiddenField control to "send" value from server to client, where you can read it from JavaScript.


    Bond... Brooke Bond!



    • Edited by Aviw_ Wednesday, March 14, 2012 2:48 PM
    • Marked as answer by Qiao Wei Friday, March 23, 2012 11:24 AM
    Wednesday, March 14, 2012 2:45 PM
  • hi

    define javascript variable like this:

    <script type="text/javascript">
    var myVariable = '<%= this.Sesstion["Foo"] %>';
    </script>

    in this example your variable will have string type. If it is e.g. integer, you don't need to use quotes ''. The idea is the same as described in this article: Pass locale of current web site (SPWeb) in Sharepoint into javascript.

    Note that you can't use this technique in separate .js files. You can only use it if javascript is embedded to the page as shown above. However it is not big limitation. You may define variable as shown above and pass it to the function which is defined in separate .js file.


    Blog - http://sadomovalex.blogspot.com
    CAML via C# - http://camlex.codeplex.com

    • Marked as answer by Qiao Wei Friday, March 23, 2012 11:24 AM
    Wednesday, March 14, 2012 2:53 PM

All replies

  • Session is a server object. You cannot read values from session using javascript (there are security reasons for that too).

    You should get value from session on server and put it to javascript (do you generate js on server?).

    Also, you may use HiddenField control to "send" value from server to client, where you can read it from JavaScript.


    Bond... Brooke Bond!



    • Edited by Aviw_ Wednesday, March 14, 2012 2:48 PM
    • Marked as answer by Qiao Wei Friday, March 23, 2012 11:24 AM
    Wednesday, March 14, 2012 2:45 PM
  • hi

    define javascript variable like this:

    <script type="text/javascript">
    var myVariable = '<%= this.Sesstion["Foo"] %>';
    </script>

    in this example your variable will have string type. If it is e.g. integer, you don't need to use quotes ''. The idea is the same as described in this article: Pass locale of current web site (SPWeb) in Sharepoint into javascript.

    Note that you can't use this technique in separate .js files. You can only use it if javascript is embedded to the page as shown above. However it is not big limitation. You may define variable as shown above and pass it to the function which is defined in separate .js file.


    Blog - http://sadomovalex.blogspot.com
    CAML via C# - http://camlex.codeplex.com

    • Marked as answer by Qiao Wei Friday, March 23, 2012 11:24 AM
    Wednesday, March 14, 2012 2:53 PM