Solved

Javascript Image Output

Posted on 2007-03-18
5
1,930 Views
Last Modified: 2007-03-19
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!=="")&&(product1!==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!
0
Comment
Question by:tags266
5 Comments
 
LVL 30

Expert Comment

by:VirusMinus
ID: 18745781
use relative paths..

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">
0
 
LVL 51

Expert Comment

by:Ryan Chong
ID: 18745786
you need put a "else" case there? try like:

if ((product1!=="")&&(product1!==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..
0
 
LVL 12

Accepted Solution

by:
Sinoj Sebastian earned 500 total points
ID: 18746613
> "<img src="item1.jpg">"
This is not a valid string. Try this
"<img src=\"item1.jpg\">"
0
 
LVL 51

Expert Comment

by:Ryan Chong
ID: 18746630
sinoj is right, or:

document.write("<img src='item1.jpg'>");

etc
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 18747133
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
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
The viewer will learn how to count occurrences of each item in an array.

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question