Solved

Javascript Image Output

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

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 49

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
jQuery detect if it is a mobile device 3 98
c# LinkButton OnClientClick 2 34
Reference Error with plugin 1 30
Split in Javascript 5 30
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

911 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now