Page 1 of 2 12 LastLast
Results 1 to 15 of 21
  1. #1
    Join Date
    Jan 2002
    Location
    Seattle, WA.
    Posts
    6

    Question Unanswered: Please Help: How do i set a session variable using javascript?

    Hello,
    I am trying to set a session variable in a javascript block and
    am having trouble. Please take a look at this code snip and
    offer any suggestions are greatly appreciated.

    var str = "test";
    "<%session("SaveSelected")=str%>";
    alert("<%=session("SaveSelected")%>");

    This code fails...why?

    Thank you
    Mark

  2. #2
    Join Date
    Jun 2003
    Location
    USA
    Posts
    1,032
    It's easy to use the value of a VBScript variable in JavaScript:
    document.writeln('<%= MyVBScriptVariable %>');

    Or here is an example which puts the value of a VBScript variable into a JavaScript alert box:
    Response.Write "<script language='JavaScript'>alert('The value is " & MyVBScriptVariable & "');</script>"

    But to send the value of a JavaScript variable to VBScript, well that's another story. You could have the JavaScript fill in a hidden form field and then post the form. That way VBScript can pick up the value of the hidden form field with Request.Form("MyFieldName")
    J. Paul Schmidt, Freelance Web and Database Developer
    www.Bullschmidt.com
    Access Database Sample, Web Database Sample, ASP Design Tips

  3. #3
    Join Date
    Jun 2003
    Location
    USA
    Posts
    1,032
    Oh yeah, interesting handle (kingpoop)! Like I should talk (Bullschmidt).
    J. Paul Schmidt, Freelance Web and Database Developer
    www.Bullschmidt.com
    Access Database Sample, Web Database Sample, ASP Design Tips

  4. #4
    Join Date
    May 2003
    Posts
    4

    Post

    "<%session("SaveSelected")=str%>";
    try this exact line BUT without the quotes!!!

  5. #5
    Join Date
    Feb 2002
    Location
    North Wales, UK
    Posts
    114
    Shouldn't there be a ";" at the end of each code line?

    PHP Code:
    <%
    session("name")='value';
    %> 
    J^ - web | email
    newsASP Developer

  6. #6
    Join Date
    Sep 2003
    Posts
    7
    I'm having a similar problem, kind of.

    I want to use javascript to empty out a session variable if one exists:
    Code:
    <SCRIPT LANGUAGE=javascript>
    		if ((window.opener.location.href) == 'http://aUrl/aPage.asp')
    			{
    				window.close();
    			}
    		else
    			{
    				if ((<%session("filestring")%>.length) == 0)//problem with this line.
    					{
    						window.close();
    					}
    				else
    				{
    					<%session("filestring") = ""%>				
    					window.close();
    				}		
    			}		
    </script>

    I'm really stuck with this now guys, I'd appreciate your help.

    Cheers
    Last edited by Bloke; 09-03-03 at 08:34.

  7. #7
    Join Date
    Jul 2003
    Location
    SoCal
    Posts
    721
    Hold on a second. JavaScript is client-side scripting, and Session variables are stored server-side. The ASP will always execute before the JavaScript. Unless you are writing server-side JavaScript, all this talk is moot.

    The basic process that page creation takes is as follows:

    1) Browser requests page.
    2) Server receives request.
    3) Server handles request (aka - gets file).
    4) In the case of ASP, if the file has the extension .asp (or other associated extension), the SERVER will execute all ASP code. In executing this ASP code a new page is created in memory (on the server) with all of the values and resulting information from the ASP page.
    5) The compiled content from executing the ASP is sent to the requesting browser.
    6) The browser renders the page.
    7) JavaScript executes.

    After step 5, the server is out of the picture unless you make another request to it (yes, there are exceptions to the rule, but for the basics, it ends there). So your JavaScript cannot communicate with the server to kill the session.

    What you CAN do though, is use JavaScript to make another request by submitting a form or changing the URL with a QueryString parameter which tells the server to kill the session.

    An example would be a banking site. After x number of seconds of inactivity, you get a popup that tells you your session is about to expire.

    The way they do this is that they have some JavaScript that checks the amount of inactive time, if it's reached, some JavaScript opens a new window to an html which tells them their session is about to expire. That HTML in the new window has javascript (or a meta refresh) that passes a parameter to the server, requesting to kill the session (which is just another ASP which removes the session and redirects the user).

    I hope that helps clear it up a bit...

  8. #8
    Join Date
    Sep 2003
    Posts
    7
    Mission accomplished. This did the trick:

    Code:
    <SCRIPT LANGUAGE=javascript>
    {
    
    var MySession = "<%=Session("filestring")%>"
    
    	if ((window.opener.location.href) == 'http://aUrl/aPage.asp')
    		{
    			window.close();
    		}
    	if ((MySession == null) || (MySession == ''))
    		{
    			window.close();
    		}
    	else
    		{
    			<%session("filestring") = ""%>;
    			window.close();
    		}		
    }	
    
    
    </script>

  9. #9
    Join Date
    Jul 2003
    Location
    SoCal
    Posts
    721
    Originally posted by Bloke
    if ((<%session("filestring")%>.length) == 0)//problem with this line.
    try adding double quotes:

    Code:
    if (("<%session("filestring")%>".length) == 0)
    .length only works with strings. Without the "quotes" around the text coming from session("filestring"), JavaScript wants to execute the value as code.

    This won't kill the session though..

  10. #10
    Join Date
    Sep 2003
    Posts
    7
    ..the final version of the code (contained in an asp page) that worked when called using onunload is:

    Code:
    <SCRIPT LANGUAGE=javascript>
    {
    
    var MySession = "<%=Session("filestring")%>"
    	if ((window.opener.location) == 'http://aURL/aPage.asp')//if we've proceeded to the next page
    		{
    			window.close();
    		}
    //	else
    //	{	
    	if ((MySession == null) || (MySession == '') || (MySession == ""))//if the page has been closed without setting any session variables
    		{
    			window.close();
    		}
    	else//the page has been closed with session variables that we want to kill
    		{
    			<%session("filestring") = ""%>;
    			window.close();
    		}		
    //	}	
    }	
    
    
    </script>
    Hope this is useful to someone else!

    Cheers

  11. #11
    Join Date
    Jul 2003
    Location
    SoCal
    Posts
    721
    Maybe I'm not understanding this, but you're running the javascript client-side right?

    I don't see how this could possibly work (except that closing all of the browsers would kill the session anyways). You cannot control ASP sessions from client-side JS. All of your ASP code will execute in the above statement regardless of the JavaScript you wrap around it, because ASP executes even before the page is sent to the browser.

    I used to teach this stuff... but hey... if you think it's working... more power to you.

  12. #12
    Join Date
    Sep 2003
    Posts
    7
    The javascript is running in an asp page which consists entirely of this code. It does work, honest! It does exactly what it says on the tin.

    Cheers

  13. #13
    Join Date
    Jul 2003
    Location
    SoCal
    Posts
    721
    But you're missing the point. JavaScript executes AFTER the ASP. They do not execute synchronously. You can change JavaScript with ASP (to a degree), but your ASP (and therefore sessions) cannot be changed by JavaScript (unless your JS is executing server-side.. which it's not if you're calling this from an onUnLoad event)..

    If I were to do this:
    Code:
    <script language="JavaScript">
    
    <%
      Dim myVar
      myVar = 2
    %>
    
    alert("<%=myVar%>");
    
    </script>
    First, the ASP will execute, so your final HTML (what the browser receives) will actually look like this:
    Code:
    <script language="JavaScript">
    
    alert("2");
    
    </script>
    You can see that there is no reference to any ASP in the final rendered ASP because it's been processed by the server before it is sent to the browser. All you see if the result of the processing of the ASP page.

    I don't believe it's working like you THINK it's working. It's working as a by-product of something else. If you're running this on an "onUnLoad" event, that means they are either going to another page (persisting the session), or closing the browser window (ending the session). If they are closing the browser window, their session will end anyways. So it works because the browser is closing, ending the session, not because of your JavaScript.

    I would try it without the line:
    Code:
    <%session("filestring") = ""%>;
    See if it still works...

  14. #14
    Join Date
    Sep 2003
    Posts
    7

    Thumbs up

    I fully understand the point, I really do. I'm telling you though that it does work. I does exactly what I wanted it to do.

    I can send you the full code if you really want however I'm happy that it does what I need it to and has overcome the problem that I had.

    I really do appreciate your help and your comments, though.

    Cheers

  15. #15
    Join Date
    Jul 2003
    Location
    SoCal
    Posts
    721
    Well, it works, but not because of the way you have your code... there are right ways to do things, and wrong ways to do things... if you're happy with how it functions, even though it's not right, that's fine... but it's simply an illusion of what is really occurring. Maybe I'm just overly concerned.. in that case, by all means, continue... but it may come back to bite you later...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •