Session Variable

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MATTXtwo
    New Member
    • Sep 2006
    • 83

    Session Variable

    Is't posibble to share session variable within asp and javascript code?
  • DrBunchman
    Recognized Expert Contributor
    • Jan 2008
    • 979

    #2
    It is possible to write a session variable to a javascript function by embedding it inside the function using server tags like this:

    Code:
    <script type="text/javascript">
    function test()
    {
    var a = '<%=Session("MySessionVar")%>';
    alert(a);
    }
    </script>
    You can only set a session variable on the server so it must be passed from your javascript to the server through a form or the querystring.

    Out of interest, why are you trying to do this?

    Hope this helps,

    Dr B

    Comment

    • MATTXtwo
      New Member
      • Sep 2006
      • 83

      #3
      Thanks it works well... but how to assign to session variable?

      Comment

      • MATTXtwo
        New Member
        • Sep 2006
        • 83

        #4
        Actually it's not working..
        I want to have the value of session variable that has define by vbcode...
        Your code is actually define it back with javascript

        Comment

        • DrBunchman
          Recognized Expert Contributor
          • Jan 2008
          • 979

          #5
          No my code is setting a javascript variable to the value of a session variable as defined in your vbscript. Try using the following example and you'll see that the value of the javascript variable 'a' is set to the value of the session variable 'MySessionVar'.

          Code:
          <%
          Session("MySessionVar") = "Hello World!"
          %>
          
          <script type="text/javascript"> 
          function test() 
          { 
          var a = '<%=Session("MySessionVar")%>'; 
          alert(a); 
          } 
          </script>
          
          <body onload="test()">
          
          </body>
          Does this make sense?

          Dr B

          Comment

          • banu2001
            New Member
            • Sep 2007
            • 2

            #6
            Session.RemoveA ll();
            string hstring = "<script language='javas cript'> window.location .href='logOut.a spx'</script>";
            Page.RegisterSt artupScript("on e", hstring);

            Comment

            • DrBunchman
              Recognized Expert Contributor
              • Jan 2008
              • 979

              #7
              banu2001,

              Why have you posted this? It looks like ASP.NET C# while this is the Classic ASP Forum and it also looks totally unrelated to the thread.

              Dr B

              Comment

              • Frinavale
                Recognized Expert Expert
                • Oct 2006
                • 9749

                #8
                Originally posted by DrBunchman
                It is possible to write a session variable to a javascript function by embedding it inside the function using server tags like this:

                Code:
                <script type="text/javascript">
                function test()
                {
                var a = '<%=Session("MySessionVar")%>';
                alert(a);
                }
                </script>
                You can only set a session variable on the server so it must be passed from your javascript to the server through a form or the querystring.

                Out of interest, why are you trying to do this?

                Hope this helps,

                Dr B
                Thanks DrBunchman :)
                I never thought about doing this and I don't know why.

                -Frinny

                Comment

                Working...