Jump to content

Image Instead


Spunky
 Share

Recommended Posts

Ok, using the script in one of the tutorials of this site, this tutorial. Can I have an image show up instead? Like instead of the document.write statement could there be something I put there that an image would show instead?

Link to comment
Share on other sites

Ok, using the script in one of the tutorials of this site, this tutorial. Can I have an image show up instead? Like instead of the document.write statement could there be something I put there that an image would show instead?
Yep, there are a few ways to do it: easiest one is to create an empty <span> or <div> element on the page and give it an id, e.g. <span id="imageHolder"> </span> and then where it says document.write, instead put:document.getElementById("imageHolder").innerHTML="<img src='whatever.jpg' alt='this was put here by JavaScript' />" (note the different kinds of quotes). Read further into the JavaScript tutorials and you'll learn all about it.
Link to comment
Share on other sites

Ok I tried that out and when the time is not less than the number I choose it shows "Good Day" like it should but when the time does meet the requirement to show image, it's just blank. Here is the code I used, I only made small changes to get the code do what I want, then I will incorporate it the way I want.

<html><head><style type="text/css"><!--#imageHolder {}--></style></head><body><script type="text/javascript">var d = new Date();var time = d.getHours();if (time < 13) {document.getElementById("imageHolder").innerHTML="<img src='test.png' alt='this was put here by JavaScript' />"}else{document.write("<b>Good day</b>");}</script><span id="imageHolder"></span><p>This example demonstrates the If...Else statement.</p><p>If the time on your browser is less than 10,you will get a "Good morning" greeting.Otherwise you will get a "Good day" greeting.</p></body></html>

Link to comment
Share on other sites

The script is running before the span exists in the document object model, so you need to either place the script after the span, or put it inside a function and call it with window.onload = whateverYouCallTheFunction() (this goes inside the script tags, but not inside the function).

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...