JavaScript Image Switch

Posted:
in Genius Bar edited January 2014
I maintain an elementary website for my school district, and i was wondering if anyone knows how i would use javascript to create a webpage that changes the current picture to a new one as of jan 1



can you do regular html inside of javascript? if you can, then i could probably just do an if date = whatever...any help or suggestions would be appreciated

Comments

  • Reply 1 of 1
    [quote]Originally posted by ast3r3x:

    <strong>I maintain an elementary website for my school district, and i was wondering if anyone knows how i would use javascript to create a webpage that changes the current picture to a new one as of jan 1



    can you do regular html inside of javascript? if you can, then i could probably just do an if date = whatever...any help or suggestions would be appreciated</strong><hr></blockquote>



    You can do just that quite easily, but make sure you test your code cross browser... I've found myself writing things before which were 1000's of years off in say Internet Explorer, but worked fine in Netscape...



    First Examine the Time object, to understand exactly under what condition you want to execute the code off of (makeing sure to correct for Internet explorer/Netscape).



    Second, You could either swap/change the image, or just have not written the immage yet...



    &lt;Script Language="JavaScript"&gt;

    //Example:



    // Do some setup code, first



    if(IE Condition || Netscape Condition)

    document.write('&lt;img ...');

    else

    document.write('&lt;img ...');



    // If Javascript isn't supported, make sure to actually still have an image



    &lt;img...

    &lt;/Script&gt;





    I reccomend using document.write to create the image because it should work for any Javascript Browser, while I usually use other methods, I don't really care about the poor saps who are affected... Caring is Something you probably have to do...



    Here's my JavaScript Tetris Clone

    <a href="http://www.acs.brockport.edu/~gbru1029/javascript/it/itjavascript.html&quot; target="_blank">http://www.acs.brockport.edu/~gbru1029/javascript/it/itjavascript.html&lt;/a&gt;



    And this page contains a counter/Timer of how many year's I've used Linux (coming toward's 3)

    <a href="http://www.itss.brockport.edu/~gbru1029/linux/linux.html"; target="_blank">http://www.itss.brockport.edu/~gbru1029/linux/linux.html</a>;



    You'll see that it works in at least Netscape 7.2 and Internet Explorer 6... (hehe, I told you I make no guarentee's of backwards compatibility of code I write...)



    I've just currently redone my JavaScript Tetris Clone by having it draw by changing the background color of table Cells... Which also made an interesting Etch-A-Sketch by utilizing on-mouse-over...
Sign In or Register to comment.