Solved

Javascript Image Output

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

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 …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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.

762 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

20 Experts available now in Live!

Get 1:1 Help Now