locked
Session variable into javascript RRS feed

  • Question

  • User-637563233 posted

    I inserted the javascript below in my aspx. It's supposed to preload some images, but the images which should be loaded depend on the session variable "model", which I insert into my src string. 

    I used a variable model and assigned the session variable to it ,but it doesn't seem to work. Does someone have an idea to do it better, or to make it work?

    thx in advance. 

     

    <script language="JavaScript">

    <!--

    var model;

    model = <%=Session("name")%>;

    if (document.images)

    {

    pic1=
    new Image(675,239);

    pic1.src="../images/personenw/modellen/small/"& model &"Big1.jpg";

    pic2= new Image(675,239);

    pic2.src="../images/personenw/modellen/small/"& model &"Big2.jpg";

    pic3= new Image(675,239); pic3.src="../images/personenw/modellen/small/"& model &"Big3.jpg";

     

    pic4= new Image(675,239);

    pic4.src="../images/personenw/modellen/small/"& model &"Big4.jpg";

    }

    //-->

    </script>

    Tuesday, January 15, 2008 7:10 AM

Answers

  • User-768391503 posted

    All of those response and no one saw this:

    all/"& model &"Big1.jpg";

     JavaScript does not use & for concatenation. It uses +

    Eric
     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 15, 2008 9:29 AM

All replies

  • User-1426124666 posted

    Write the javascript to a string in your code behind page (appending the session variable to a string containing the javascript), and then register the string to the page by using the ClientScript.RegisterClientScriptBlock method. 

    Tuesday, January 15, 2008 7:25 AM
  • User-637563233 posted

     How do I write javascript in my aspx.vb?

    And how do u use ClientScript.RegisterClientScriptBlock?

    which values do I need to adress to the variables in that block?

     


     
     

    Tuesday, January 15, 2008 8:30 AM
  • User-1426124666 posted

     

    First of all you read the help file as it shows you how to use it.

    which values do I need to adress to the variables in that block?
     

    Again, from the help file:

     

    Public Sub RegisterClientScriptBlock ( _
        type As Type, _
        key As String, _
        script As String _
    )
      
    Tuesday, January 15, 2008 8:42 AM
  • User798903548 posted

    I believe that your problem is right here:
         var model;
         model = <%=Session("name")%>;

    Try:
         var model = '<%= Session("name") %>';

    Note the single quotes.

    NC...

    Tuesday, January 15, 2008 8:51 AM
  • User-1363023537 posted

    in code behind declare public local  variable

     public string name = (string)session["name"];

    in javascript write

    Try
    {
     var model = '<%=name%>' ;

    catch(e) 

    {
     

    Satalaj.

    Tuesday, January 15, 2008 9:00 AM
  • User-637563233 posted

    Also with the single quotes the preload isn't happening :/

    I think javascript doesn't accept the string breaking?

     
    With the other solution I'm clueless waht to do in the .aspx.vb file.
    I put the javascript into a string.

    I set the key as ""

    but I dunno what to do with the type.

    I got smt like this now:

     

    Public Sub RegisterClientScriptBlock( _
        ByVal type As Type, _
        ByVal key As String, _
        ByVal script As String _
    )
            Dim instance As ClientScriptManager
            Dim soort As Type
            Dim sleutel As String = ""
            Dim PreloadScript As String = "<script language='JavaScript'><!-- If (document.images) Then { pic1= new Image(675,239);pic1.src='../images/personenw/modellen/small/" & Session("model") & "Big1.jpg';pic2= new Image(675,239);pic2.src='../images/personenw/modellen/small/" & Session("model") & "Big2.jpg';pic3= new Image(675,239);pic3.src='../images/personenw/modellen/small/" & Session("model") & "Big3.jpg';pic4= new Image(675,239);pic4.src='../images/personenw/modellen/small/" & Session("model") & "Big4.jpg';}//--></script>"

            instance.RegisterClientScriptBlock(soort, _
                sleutel, PreloadScript)
        End Sub
     

     

    Tuesday, January 15, 2008 9:16 AM
  • User-768391503 posted

    All of those response and no one saw this:

    all/"& model &"Big1.jpg";

     JavaScript does not use & for concatenation. It uses +

    Eric
     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 15, 2008 9:29 AM
  • User798903548 posted

    Good catch Eric! I didn't read that far down!

    NC...

    Tuesday, January 15, 2008 9:38 AM
  • User798903548 posted

    Also, I don't believe that using "~" will resolve in JavaScript. You'd have to fully qualify the image path (http://server-name/images/...).

    NC...

    Tuesday, January 15, 2008 9:42 AM
  • User-637563233 posted

    the "+" did it!  -.-

    ty v much,

     

     

    Tuesday, January 15, 2008 9:47 AM
  • User-751708624 posted

    Much context is missing in all of the quick answers...  

    One example:  
    The construct <%...%> only executes on render of the page, thus if you are using it in an onclick event the result is nothing.
    So, for instance if a user was to come back to a page via the BACK/FORWARD buttons on the browser, this approach royally fails.
    ...and no clue as to why the variable is empty. 

    Sunday, September 11, 2011 6:16 PM