Solved

Javascript Image Output

Posted on 2007-03-18
5
1,970 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 52

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 52

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

617 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