Dynamic IMG (tag) using javascript


/ Published in: JavaScript
Save to your folder(s)

If you need to have an tag be dynamic (in this case, I wanted to reference my weather station graphs from Weather Underground and ensure I had "today's" graph without updating the HTML by hand), then this is the way to do it. I made it more verbose just for illustration. You can reduce the code-size dramatically if that's important to you.


Copy this code and paste it in your HTML
  1. <img src="" id="graphs"/><br/>
  2. <script>
  3. d = new Date();
  4.  
  5. year = d.getFullYear();
  6. day = d.getDate();
  7. month = d.getMonth() + 1;
  8.  
  9. DAY = day.toString() ;
  10. MONTH = month.toString();
  11. YEAR = year.toString();
  12.  
  13. document.getElementById("graphs").src = "http://www.wunderground.com/cgi-bin/wxStationG
  14. raphAll?day=" + DAY + "&year=" + YEAR + "&month=" + MONTH + "&ID=KMEBERWI7&type=3&width=500
  15. &showtemp=1&showpressure=1&showwind=1&showwinddir=1&showrain=1" ;
  16. </script>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.