Results 1 to 10 of 10

Thread: Javascript If...if else statement with HTML code

  1. #1
    Join Date
    Nov 2006
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Javascript If...if else statement with HTML code

    Hi there,

    I've got 3 images that I want to show at different times of the day.

    I've used a standard Javascript If else statement e.g.:

    <script type="text/javascript">
    var d = new Date()
    var time = d.getHours()

    if (time<12)
    {
    document.write("Morning")
    }
    else if (time>12 && time<17)
    {
    document.write("Afternoon")
    }
    else
    {
    document.write("Evening")
    }
    </script>


    ... and this code works. I've put this where the image(s) need to be displayed. What I want to do is replace the "document.write" bit with a standard <IMG> (or equivalent) where it'll just put the image in, just so a different image will be displayed

    I've tried all sorts of different variations with no luck. I'm new to Javascript, so you'll probably think i'm dumb. Any ideas?

  2. #2
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    question: did you try using document.write("<img src='whatever.jpg'>"); ?

    If not that is why it is not working for you. The document.write function in Javascript just does what it says (writes in the document.). Go ahead and try this and see if it fixes your problem.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  3. #3
    Join Date
    Nov 2006
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by thetestingsite View Post
    question: did you try using document.write("<img src='whatever.jpg'>"); ?

    If not that is why it is not working for you. The document.write function in Javascript just does what it says (writes in the document.). Go ahead and try this and see if it fixes your problem.
    Hello, thanks for replying.

    Yes, I did try that, and that's what I thought it would do - which is why I chose that bit of code, but it doesn't seem to display any of the images.

    I could understand if there was an error in the code, which means none of the If statements were ever true, but surely then the else statement should be shown. The javascript is correct isn't it - there's nothing wrong in there?

  4. #4
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    Theres nothing wrong with the javascript from what i can see in the first post above. Can you post a link to the website that the javascript is on and I'll take a look at it. If you don't want to post it you could also send it via PM or YIM to me.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  5. #5
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    I may be off here, but I think I've heard that document.write doesn't work after the page is loaded... and before that replaces everything with it's contents.
    You should use another command for the image.

    something like, perhaps...
    <img src="image.jpg" name="testimage">

    javascript:
    testimage.src = "afternoon.jpg";

    But.... I don't know if that works.

    Someone shall know soon...

    Since I don't know javascript, I can't be much help. Heh.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  6. #6
    Join Date
    Nov 2006
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Many thanks both for your help.

    You actually answered it in your first reply thetestingsite, but it was something I hadn't noticed.

    The standard speech marks (") around an image source do not work within the document.write speech marks. Instead - as you suggested, inverted commas (') have solved the problem.

    Many thanks!

  7. #7
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    not a problem at all, let us know if you need anymore help
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  8. #8
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by mattsmith24 View Post
    if (time<12)
    /* ... */
    else if (time>12 && time<17)
    What about at noon?

    Mike

  9. #9
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Then, according to the code, it's "Evening"


    The solution is just to remove the first part of the if:
    else if (time<17)

    That works since you've already eliminated each time before noon.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  10. #10
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    However, note that this doesn't allow for non-JS browsers. The preferable option is:
    Code:
    <img src="default.png" alt="Time Image" id="image_id">
    <script type="text/javascript">
    var imgs = {
      0 : "morning.png",
      12 : "afternoon.png",
      17 : "evening.png"
    },
      time = (new Date()).getHours();
    
    var li = 0;
    for(var i in imgs)
      if(i > time)
        break;
      else
        li = i;
    
    window.onload = function() {
      document.images['image_id'].src = imgs[li];
    };
    </script>
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •