SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Jun 2005
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Javascript variables

    I need to display an image which has a filename based on the date. Easy enough to get the month/day/year variables but I can't figure out the proper format to concatenate them into a string to define the image source...

    <img src="year+month+today"+.gif>

    Can anyone tell me the correct layout for this?

  2. #2
    SitePoint Wizard silver trophy rushiku's Avatar
    Join Date
    Dec 2003
    Location
    A van down by the river
    Posts
    2,056
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    "year+month+today" creates a string that is literally "year+month+today", you want to access the variables with those names, so drop the quotes - done.

  3. #3
    SitePoint Member
    Join Date
    Jun 2005
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thx Rushiko but it didn't work for me {I'm not a programmer} so let me get specific. I'm creating a page that will reside on my computer only. It's meant to be a morning news kinda thing with some comics.

    Here's my code, and I'm doing something else wrong because I get a script error message...

    <script type="text/javascript">
    var site="http://images.ucomics.com/comics/";
    var strip="tmhel/";
    var thedate=new Date();
    var yr=thedate.getFullYear()+"/";
    var smlyr=yr.substring(2,5);
    var mth=thedate.getMonth()+1;
    if mth < 10
    {

    mth="0"+mth;

    }
    var dy=thedate.getDate();
    if dy < 10
    {

    dy="0"+dy;

    }
    </script>
    <img src=site+strip+yr+"/tmhel"+smlyr+mth+dy>

    Thanks again for helping.

  4. #4
    SitePoint Member
    Join Date
    Jun 2005
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Bump. Can anyone help me with this?

  5. #5
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    <img src=site+strip+yr+"/tmhel"+smlyr+mth+dy>
    Cannot use JavaScript variables outside of a JavaScript code block.

    Use of document.write() is discouraged, however, since you haven't provided enough code for me to give you the answer you are looking for otherwise, I'll use it here.

    I'm assuming that you are doing the various calculations to determine the path correctly.

    Code:
    <script type="text/javascript">
    var site="http://images.ucomics.com/comics/";
    var strip="tmhel/";
    var thedate=new Date();
    var yr=thedate.getFullYear()+"/";
    var smlyr=yr.substring(2,5);
    var mth=thedate.getMonth()+1;
    if mth < 10
    {
    
    mth="0"+mth;
    
    }
    var dy=thedate.getDate();
    if dy < 10
    {
    
    dy="0"+dy;
    
    }
    document.write('<img src="'+site+strip+yr+'/tmhel'+smlyr+mth+dy+'" alt="">');
    
    </script>
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.


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
  •