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 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Display a time-dependant image when opened with a browser?

    My question is twofold. First, is there a way to have a javascript file display an externally hosted image when opened with a browser? I mean such a way where the URL of the javascript file can be referenced by an html document and display an image?

    Furthermore, is there a way to have this be a different image depending on the time of day and timezone of the user, specifically one image in the morning and a different image in the evening?

    I apologize if I posted this in the wrong section, but I wasn't sure where else this could go.
    Last edited by Ardosos; 01-31-2012 at 04:27 PM.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,918
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Yes, of course.

    Code:
    <img id = "myimage">
    
    <script type = "text/javascript">
    var nowTime = new Date().getHours();
    if (nowTime <12) {
    document.getElementById("myimage").src="http://www.vicsjavascripts.org.uk/StdImages/Egypt5.jpg";
    }
    else {
    document.getElementById("myimage").src="http://www.vicsjavascripts.org.uk/StdImages/Egypt6.jpg";
    }
    
    </script>
    I don't understand your reference to time zone. Javascript will take the time according to the user's clock, so the morning image will show before 1200 wherever in the world the user is (assuming his clock is correct).

    But be aware that other peoples' images are strictly copyright and you ought to get permission if you want to display them. And some people block "hot linking" (sometimes called bandwidth theft) of their images. http://www.javascriptkit.com/howto/htaccess10.shtml


    Well, we are not out of the wood yet, but we can see the trees. - Commentator Blackburn Rovers Online
    Last edited by Philip M; 01-31-2012 at 08:24 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    Ardosos (01-31-2012)

  • #3
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thank you, exactly what I needed.


  •  

    Posting Permissions

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