Link to home
Start Free TrialLog in
Avatar of faughey
faughey

asked on

I need an <img src> filename to be variable driven based on current date

Here is a snippet of my code:

<script type="text/javascript">
   var today = new Date()
   var day = today.getDate()
   var month = today.getMonth() + 1
   var year = today.getYear()
   vday = day.toString()
   vmonth = month.toString()
   vyear = year.toString()
   if (vday.length == 1) {vday = '0' + vday}
   if (vmonth.length == 1) {vmonth = '0' + vmonth}
   document.write(vyear + vmonth + vday)
   var vFileName="http://www.mydomain" + vyear + vmonth + vday + ".gif"
   document.write(vFileName)
 </script>
  <p align="left"><img src="http://www.mydomain20050429.gif">  ' This works
  <p align="left"><img src='"http://www.mydomain' + vyear + vmonth + vday + '".gif"'>  ' This doesn't work
  <p align="left"><img src= vFileName > ' This doesn't work


Any suggestions?

TIA
Avatar of Batalf
Batalf
Flag of United States of America image

Try to set the src of the image afterwards. Example:


<p align="left"><img id='myImage' src=''>
<script type="text/javascript">
   var today = new Date()
   var day = today.getDate()
   var month = today.getMonth() + 1
   var year = today.getYear()
   vday = day.toString()
   vmonth = month.toString()
   vyear = year.toString()
   if (vday.length == 1) {vday = '0' + vday}
   if (vmonth.length == 1) {vmonth = '0' + vmonth}
   document.write(vyear + vmonth + vday)
   var vFileName="http://www.mydomain/" + vyear + vmonth + vday + ".gif"
   document.getElementById('myImage').src = vFileName;

 </script>
Avatar of faughey
faughey

ASKER

Thanks, worked great!
Glad I could help!

Batalf
ASKER CERTIFIED SOLUTION
Avatar of Batalf
Batalf
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial