Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 67
  • Last Modified:

Default image used when variable is not found

For an HTML based Webpage ( No CGI access btw)
EXAMPLE: (STARTS)
<body>
<script>
var mydate=new Date()
var day=mydate.getDay()
var month=mydate.getMonth()+1
if (month < 10)
month="0"+month
var day=mydate.getDate()
var newimg="http://www.myhomepage.com/img/today"+month+day+".gif"

document.write('<img src="`newimg`">`)
</script>
EXAMPLE: (ENDS)
This script works fine, as long as I have an image with the date in the name of the image in the "img" folder,
for example a file called "today1225.gif" is an image for christmas
What I want is to change this so that if there is no image in the img folder to use on that day, it uses a "default.gif" instead.
I don't want to use something like an alt text, I'd rather have an alternative default.gif

I was hoping there is a way to do this without making a database in the script of every image in the folder.
A javascript equal too... document.write('<img src="./img/today"+month+day+".gif alt img="./img/default.gif">`)
but the command "alt img" isn't real.

Any Ideas?

0
ALEx604
Asked:
ALEx604
1 Solution
 
ZvonkoSystems architectCommented:
Do it like this:

document.write('<img src="'+newimg+'" onError="this.src=\'./img/default.gif\';">')

0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now