You must Sign In to post a response.
  • Category: JavaScript

    How to set variable values to session using javascript

    Hi friends,
    how to set variable values to session using javascript.

    ex:
    var value ="sam,yan,yogu";

    i wana assign the variable "value" to session variable using javascript or jquery..


    thanks,
    saravanan.k
  • #756251
    Hi,
    I think you are are confused with the use of session. You cannot assign values directly to session using JavaScript. If that is possible then there will be a huge security issue. The reason whatever is there in the JavaScript can be accessible if you open View Source If you are able to fetch session data then complete data in the session will be displayed in the browser.
    So you need to have a server side code somewhere to assign value to session.


    Regards,
    Asheej T K

  • #756263
    Try the below sample code to read the session data,


    <script type="text/javascript">

    var yourVariable = '<%= Session["SessionKey"] %>';

    </script>


    Thanks & Regards
    Anil Kumar Pandey
    Microsoft MVP, DNS MVM

  • #764069
    You can try using ajax with json datatype as follows:

    function xyz() {
    var szValue = "SessionValue";
    $.ajax({
    type: "POST", url: "/WebFormName.aspx/WebFormMethodName",
    dataType: "json",
    data: szValue,
    contentType: "application/json; charset=utf-8",
    error: function (error) {
    alert("Operation failed!!!");
    }
    });
    }


    //... Inside your WebFormName.aspx.cs add following method

    public string WebFormMethodName(string szValue)
    {
    HttpContext.Current.Session["abc"] = szValue;
    }

  • #765753
    Hi

    Session is the thing which comes under server side and browser dependent ,
    In client side we can keep value in browser by two ways -- localstorage and sessionstorage
    LocalStorage -- its domain dependent ie if you close the browser and reopen stored value will remain same

    eg :
    Set : localStorage.setItem("myname", "abc");
    Get : localStorage.getItem("myname");

    Sessionstorage -- This is tab dependent ie when we close the tab/ browser then stored value will deleted automatically

    eg :
    Set : sessionStorage.setItem('myname', 'abc');
    Get : sessionStorage.getItem('myname');


  • Sign In to post your comments