tags266
asked on
Javascript Image Output
Hello everybody - I'm somewhat new to Javascript, and am scripting an elementary webstore. I am writing an if statement, that if true, will output a line of text, and an image. When the statement is true, the text string outputs just fine. I just can't get the image to show.This is a snippet of my code:
if ((product1!=="")&&(product 1!==null)) {
document.writeln("<br /><br />Slaughterhouse Five, by Kurt Vonnegut - Quantity: "+product1+" @ $15.00 Each");
document.write("<img src="item1.jpg">")
}
When this if statement is true, the text outputs fine, but the image just doesn't show up. I don't even get a broken image - it's like the code doesn't even bother looking for it. Is there another way to load the pic, preferably by directly referencing the path on my server where it's located? I'm only writing 3 of these statements per page, so I'd like to avoid arrays if possible.
Thanks!
if ((product1!=="")&&(product
document.writeln("<br /><br />Slaughterhouse Five, by Kurt Vonnegut - Quantity: "+product1+" @ $15.00 Each");
document.write("<img src="item1.jpg">")
}
When this if statement is true, the text outputs fine, but the image just doesn't show up. I don't even get a broken image - it's like the code doesn't even bother looking for it. Is there another way to load the pic, preferably by directly referencing the path on my server where it's located? I'm only writing 3 of these statements per page, so I'd like to avoid arrays if possible.
Thanks!
you need put a "else" case there? try like:
if ((product1!=="")&&(product 1!==null)) {
document.writeln("<br /><br />Slaughterhouse Five, by Kurt Vonnegut - Quantity: "+product1+" @ $15.00 Each");
} else {
document.write("<img src="item1.jpg">")
}
see whether it works or not..
if ((product1!=="")&&(product
document.writeln("<br /><br />Slaughterhouse Five, by Kurt Vonnegut - Quantity: "+product1+" @ $15.00 Each");
} else {
document.write("<img src="item1.jpg">")
}
see whether it works or not..
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
sinoj is right, or:
document.write("<img src='item1.jpg'>");
etc
document.write("<img src='item1.jpg'>");
etc
Also your test should be != and not !==
Also make sure you do NOT write after load of the page
<script>
function writeThis(product1) {
if ((product1!="")&&(product1 !=null)){
document.writeln("<br /><br />Slaughterhouse Five, by Kurt Vonnegut - Quantity: "+product1+" @ $15.00 Each");
document.write('<img src="item1.jpg">')
}
}
writeThis('2'); // this is OK
</script>
<a href="#" onClick="writeThis('2'); return false">NOT OK</a>
since you may NEVER do a document.write after the page have loaded
Also make sure you do NOT write after load of the page
<script>
function writeThis(product1) {
if ((product1!="")&&(product1
document.writeln("<br /><br />Slaughterhouse Five, by Kurt Vonnegut - Quantity: "+product1+" @ $15.00 Each");
document.write('<img src="item1.jpg">')
}
}
writeThis('2'); // this is OK
</script>
<a href="#" onClick="writeThis('2'); return false">NOT OK</a>
since you may NEVER do a document.write after the page have loaded
test the img in a static page without writing the image out in javascript..
if you're website is in folder 'mywebsite' and your images are in folder 'mywebsite/images'
then a page in 'mywebsite' say index.htm could have the image written out like this:
<img src="images/item1.jpg">