Results 1 to 4 of 4

Thread: Putting a random image in a Div with JavaScript

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

    Default Putting a random image in a Div with JavaScript

    The code below posts a random background image to a Div with the ID "container". I am trying to change that code so that the image posts as a regular in-line image and am struggling with the syntax. Is there a way to modify this code to do that or am I barking up the wrong tree? My ultimate objective is to put a random image in the Div that resizes to fit the browser window. My understanding is that can be done with CSS with an image. Unfortunately, the intranet that hosts the site I am working on is I.E. 8 only so I cannot take advantage of the nifty "cover" langauge in CSS 3 which would allow me to do it with a background image.


    var imgCount = 4;
    var dir = 'images/background_photos/11_Nov/';
    var randomCount = Math.round(Math.random() * (imgCount - 1)) + 1;
    var images = new Array
    images[1] = "image1.jpg",
    images[2] = "image2.jpg",
    images[3] = "image3.jpg",
    images[4] = "image4.jpg",
    document.getElementById("container").style.backgroundImage = "url(" + dir + images[randomCount] + ")";

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    You could use IE 8's proprietary filter alpha image loader (different than the simpler alpha opacity filter used for fading in/out prior to IE's adoption (in v 9) of the standard css opacity property). The alpha image loader will take an image and apply it to a div as a sort of background image (but not as the background image in css, in fact the div must have no background for this to work), and with other differences - as properties of the filter, you can choose a src and sizing method, one of the later is scale, which (if the div takes up all of the window) should work out. Other differences include a requirement that the div have 'layout' (this is a slightly different concept than standard HTML layout and is defined by MS in several places. Generally, defining both height and width in css will do it. There are other methods of establishing this special kind of 'layout' if that's inconvenient, or insufficient (as it sometimes can be). And since it's a filter, you will need to use the proprietary filter syntax in the correct manner for it to work. For more info on the filter (including it's use in css and scripting) see:

    http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx

    If I get a chance I'll play around with this and see if I can come up with more specifics.
    Last edited by jscheuer1; 12-03-2014 at 03:39 AM. Reason: English Usage
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    OK, in IE 8, this should work out quite nicely:

    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <style type="text/css">
    html, body {
    	margin: 0;
    	height: 100%;
    }
    #container {
    	width: 100%;
    	height: 100%;
    }
    </style>
    </head>
    <body>
    <div id="container"></div>
    <script type="text/javascript">
    var imgCount = 4;
            var dir = 'images/background_photos/11_Nov/';
            var randomCount = Math.round(Math.random() * (imgCount - 1)) + 1;
            var images = new Array
                    images[1] = "image1.jpg", 
                    images[2] = "image2.jpg", 
                    images[3] = "image3.jpg", 
    		images[4] = "image4.jpg", 
    document.getElementById("container").style.filter =
    "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + dir + images[randomCount] + "', sizingMethod='scale')";
    </script>
    </body>
    </html>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  4. The Following User Says Thank You to jscheuer1 For This Useful Post:

    Danl0013 (12-03-2014)

  5. #4
    Join Date
    Nov 2014
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    That solution worked perfectly. It allows the Div to be placed anywhere on the page and have any size (as long as both height and width are defined). It also scaled up nicely as a full-screen random background image. I found numerous workarounds for this scaling problem in I.E. 8 but this is the only one that worked with random images. Thanks!

Similar Threads

  1. Putting All Levels Nav Menu in a Javascript file?
    By macarthur in forum Dynamic Drive scripts help
    Replies: 5
    Last Post: 01-19-2013, 07:41 AM
  2. Replies: 12
    Last Post: 01-25-2009, 04:57 PM
  3. Putting Javascript in seperate .js page rather than in <head>
    By flyingpig in forum Dynamic Drive scripts help
    Replies: 2
    Last Post: 07-11-2008, 04:39 PM
  4. Replies: 2
    Last Post: 09-08-2006, 09:20 PM
  5. Replies: 2
    Last Post: 09-07-2006, 04:34 PM

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
  •