Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder ubik's Avatar
    Join Date
    Oct 2005
    Location
    westCoast, usa
    Posts
    347
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post date image switcher

    could someone please tell me whats wrong with this script? i have a feeling it has to do with the getElementById ? im still a new-b so any help appreciated, thanks:

    Code:
    <html>
    <head>
    
    <script language="javascript">
    today = new Date();
    christmas = new Date(2005,11,25);
    
    function switcher() {
    if (today==christmas) {
    document.getElementById("logo").src="logo1.jpg";
    	}
    }
    </script>
    </head>
    
    <body onload="switcher()">
    <center>
    <img id="logo" src="neutral.jpg">
    
    </body>
    </html>
    "True knowledge exists in knowing that you know nothing."
    -Socrates

  • #2
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,517
    Thanks
    3
    Thanked 506 Times in 493 Posts
    Code:
    <html>
    <head>
    
    <script language="javascript">
    today = new Date();
    today = new Date(today.getFullYear(),today.getMonth(),today.getDate()); // else only true foe a milli sec
    christmas = new Date(2005,11,25);
    
    function switcher() {
     if (today-christmas==0) {
      alert('its christmas')
      document.getElementById("logo").src="logo1.jpg";
     }
    }
    </script>
    </head>
    
    <body onload="switcher()">
    <center>
    <img id="logo" src="neutral.jpg">
    
    </body>
    </html>

  • #3
    Regular Coder ubik's Avatar
    Join Date
    Oct 2005
    Location
    westCoast, usa
    Posts
    347
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thank you very much vwphillips, it works very nicely. I would however like to further my knowledge of javascript, and if someone wouldn't mind as to explain to me what exactly is the purpose of the red hyphen in the following line of code, and why are they equaling 0?:

    Code:
    if (today-christmas==0) {

    thanks.
    "True knowledge exists in knowing that you know nothing."
    -Socrates

  • #4
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,517
    Thanks
    3
    Thanked 506 Times in 493 Posts
    if (today-christmas==0) they are the same

  • #5
    Regular Coder ubik's Avatar
    Join Date
    Oct 2005
    Location
    westCoast, usa
    Posts
    347
    Thanks
    0
    Thanked 0 Times in 0 Posts
    oh thats freakin awesome!, lol. it just came to me now that your subtracting the dates in milliseconds. wow that impressed me thanks again.
    "True knowledge exists in knowing that you know nothing."
    -Socrates

  • #6
    Regular Coder ubik's Avatar
    Join Date
    Oct 2005
    Location
    westCoast, usa
    Posts
    347
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I only have one more question..

    How is it that:
    Code:
    if (today==christmas)
    is not true

    but:
    Code:
     if (today-christmas==0)
    is true

    how is it that they are not equal to each other, yet when they are subtracted from each other they equal 0 ?
    "True knowledge exists in knowing that you know nothing."
    -Socrates

  • #7
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,517
    Thanks
    3
    Thanked 506 Times in 493 Posts
    they have the same value but they are not the same object

    could have used

    if (today.getTime()==christmas.getTime())

    but requires more typing

    the

    '-' does this by auto typing (uses the metod appropriate to '-')

  • #8
    Regular Coder ubik's Avatar
    Join Date
    Oct 2005
    Location
    westCoast, usa
    Posts
    347
    Thanks
    0
    Thanked 0 Times in 0 Posts
    oh, ok. thanks again.
    "True knowledge exists in knowing that you know nothing."
    -Socrates


  •  

    Posting Permissions

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