Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Display an image through java script

Status
Not open for further replies.

3allawy

Programmer
Oct 19, 2005
28
CA
Hi,

I am new to the java script world and i have a simple question. How can I display an image to the screen through java script. I've tried to embed html in java script but i it didn't work. I am pretty sure it is a simple matter but beleive it or not guys I spent two days on this issue and cannot resolve. It would be really helpful if you can send me an example.

Please Advise.

Ali Haidar
 
Something like
Code:
<script language="javascript">
  var myimg = "<img src='steps.gif'>"
  document.write(myimg);
</script>

or more simply
Code:
<script language="javascript">
  document.write("<img src='steps.gif'>");
</script>

Paranoid? ME?? WHO WANTS TO KNOW????
 
thx alot for your help...Things are getting better but I cannot include the alt="" width="650" height="106" border="0" to align the image. Is there a way to do that in java script.

Sincerley,

Ali Haidar
 
but I cannot include the alt="" width="650" height="106" border="0" to align the image

Why not?

Code:
<script language="javascript">
  document.write('<img src="steps.gif" [b]alt="" width="650" height="106" border="0"[/b]>');
</script>

If this will not work for you then perhaps you could give a bit more detail about what you're trying to accomplish. You asked for a way to embed HTML into your javascript to display your image. theniteowl was gracious enough to provide you with exactly that, yet you are giving more details to the problem after his post. Please provide all details in the original post and we can spend less time guessing what you're trying to do.

-kaht

How much you wanna make a bet I can throw a football over them mountains?
sheepico.jpg
 
Sure you can add those. The problem is you have to watch out for your quotes. If you use document.write("<img src=" that second quote just closed off your string.
You either have to use different quotes like:
document.write("<img src='myimage.gif'>");
or reverse that:
document.write('<img src="myimage.gif">');
Or use the backslash as an escape like this:
document.write("<img src=\"myimage.gif\">");

The backslash tells javascript to ignore the quote following it so it does not get interpreted as the end of the string you are entering. Without using it javascript sees the line more like this:
document.write("<img src="
myimage.gif
">"
Thinking the text between the two quotes is what you intended as it's own string. This of course will give you errors in your javascript.





Paranoid? ME?? WHO WANTS TO KNOW????
 
thx guys one more time...sorry for not giving details. The thing I want to do is to display the header image through java script. As theniteowl advised, I was able to see parts of the image but the image was not aligneed properly, so this is why I assumed that the the alt="" width="650" height="106" border="0" is not working... If I just use html then this html line will show my image correctly:
<div id="photo"><img src="images/photos/01.jpg" alt="" width="650" height="106" border="0" /></div>

However, I've tried your suggestions but still it didn't work.

Thx for ur patience.

Ali Haidae
 
document.write('<div id="photo"><img src="images/photos/01.jpg" alt="" width="650" height="106" border="0" /></div>');


Paranoid? ME?? WHO WANTS TO KNOW????
 
YUP your the man...it worked...thx a million

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top