SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Jan 2005
    Location
    Tokyo
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Trying to display a javascript variable

    I am trying to display an image using a session variable which holds the current image name

    --> var picnum;

    Doing a --> document.write(picnum); shows this to hold the correct value (eg. pic1.jpg)

    Then trying to display it using

    --> <img src='picnum';>

    and several thousand other combinations don't seem to work.

    What is the correct syntax to display this javascript variable as an image on the page?

    Also, I am able to pass a .asp session variable to a js variable using...

    --> var picnum = '<%=session("picnum")%>';

    ...is there any way to pass a js variable to a .asp session variable?

    Thanks

  2. #2
    He's No Good To Me Dead silver trophybronze trophy stymiee's Avatar
    Join Date
    Feb 2003
    Location
    Slave I
    Posts
    23,424
    Mentioned
    2 Post(s)
    Tagged
    1 Thread(s)
    Code:
    document.write('<img src="'+picnum+'">');

  3. #3
    SitePoint Member
    Join Date
    Jan 2005
    Location
    Tokyo
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks stymiee. Not quite sure how to use that though...

    Here's code that currently works, using a .asp session variable:

    <img name="stage" border="0" src='<%=session("picnum")%>' style="filter: revealtrans();">

    Trouble is, I'm wanting to use a javascript variable instead of the .asp session variable because I can change the js var from within a js function, but I don't believe I can change the .asp session value from within a js function, and I _really_ want to use a js function

  4. #4
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Then trying to display it using

    --> <img src='picnum';>

    and several thousand other combinations don't seem to work.
    Generally, javascript only has meaning inside <script> tags or inline event handlers, so using a javascript variable as the value for the html 'src' attribute is nonsensical.

    In addition, you cannot use javascript to refer to the src attribute of an image until after the page has loaded and the element exists.

    Try this:
    Code:
    <html>
    <head><title></title></head>
    <script type="text/javascript" language="javascript">
    <!-- Hide from browsers without javascript
    
    var picnum = "your_image.jpg"; //this assumes the image is in the same directory as the js file
    window.onload=function()
    {
    	document.images["my_image"].src = picnum;
    };
    
    // End hiding -->
    </script>
    </head>
    <body>
    
    <img name="my_image" src="default.htm" width="640" height="480" alt="" />
    
    </body>
    </html>


Bookmarks

Posting Permissions

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