locked
Get session variable in javascript RRS feed

  • Question

  • User-893909810 posted

    Post split off from http://forums.asp.net/t/1106637.aspx

    Hi ,

    I m having the same problem  but i want to check the session value on browser back button

    so for that i m checking this in window.onload event , but i m not getting the session value inside that event.

    pls help me!


    thanks in advance!


    Tuesday, July 21, 2009 11:40 AM

Answers

  • User798903548 posted

    The window.onload event is a client-side event and Session values are server-side variables. The two have no connection between them. You can read the value client-side, but there is no way to change it without going back to the server.

    <script type="text/javascript">
    <!--
    function windowOnLoad()
    {
     var sessionValue = '<%= Session["SomeKeyName"] %>';
     alert(sessionValue);
    }

    window.onload = windowOnLoad;
    // -->
    </script>

    Also remember that the browser Back button displays a cached version of the page, so you'll probably not have a changed value. See http://aspalliance.com/687 for info on the Refresh button which is a similar problem.

    NC...

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 21, 2009 1:31 PM

All replies

  • User-1675817941 posted

     Hi,

    use this

    var userID ="<%=Session("userID")%>";

    for reference
    http://forums.asp.net/thread/1685859.aspx

    Thanks :)

     

    Tuesday, July 21, 2009 1:17 PM
  • User-1171043462 posted

     This way

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head id="Head1" runat="server">
        <title>Untitled Page</title>
       <script type = "text/javascript">
        function GetSession()
        {
            var data = '<%=Session["Data"].ToString()%>'
            alert(data);
            return false;
        }
    </script> 
    </head>
    <body>
        <form id="form1" runat="server" style="height:100%; width:100%;" >
            <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick = "return GetSession()" />
        </form>
    </body>
    </html>


     

    Tuesday, July 21, 2009 1:28 PM
  • User798903548 posted

    The window.onload event is a client-side event and Session values are server-side variables. The two have no connection between them. You can read the value client-side, but there is no way to change it without going back to the server.

    <script type="text/javascript">
    <!--
    function windowOnLoad()
    {
     var sessionValue = '<%= Session["SomeKeyName"] %>';
     alert(sessionValue);
    }

    window.onload = windowOnLoad;
    // -->
    </script>

    Also remember that the browser Back button displays a cached version of the page, so you'll probably not have a changed value. See http://aspalliance.com/687 for info on the Refresh button which is a similar problem.

    NC...

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 21, 2009 1:31 PM
  • User-1171043462 posted

     If you want to redirect on session timeouts you can refer here

    http://www.aspsnippets.com/post/2009/06/10/Reload-Refresh-and-Redirect-Pages-using-Meta-Tags-in-ASPNet.aspx

    Tuesday, July 21, 2009 1:38 PM