Solved

Javascript Image Output

Posted on 2007-03-18
5
1,911 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 50

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 50

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

839 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