www.webdeveloper.com
Results 1 to 10 of 10

Thread: Dynamically changing background images works locally, but intermittent online.

  1. #1
    Join Date
    Oct 2012
    Posts
    15

    Dynamically changing background images works locally, but intermittent online.

    Hi, I have a problem with changing background images.
    This code works perfectly running it local but seems to work when it wants to coming from the server.
    Anyone know why, and how to fix it?


    Code:
    var images = ['reddoor.jpg', 'oldwindow2.jpg', 'stainedglass.jpg', 'bluestripe.jpg', 'cueball3.jpg', 'bodyimage.jpg', 'redbody.jpg', 'oldwindow1.jpg', 'nighttime.jpg' ];
    
    
    
    function getBackground(){
            var random = Math.floor(Math.random() * images.length);
            var imgurl = images[random];
            document.body.style.backgroundImage = 'url(' + imgurl + ')';
           
    }

    Thanks.

  2. #2
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,504
    First and obvious issue is image data size.

    How big are the images in filesize?
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

  3. #3
    Join Date
    Oct 2012
    Posts
    15
    Small images that will be tiled in the body background, none over 20kb.
    I would rather just have the images display in order of the array than random but I can't make that work either, the last image in the array always comes up. I've tried js and jquery and different methods but always get the last image. What is up with that?

  4. #4
    Join Date
    Mar 2009
    Posts
    522
    What does the rest of your code look like? What calls the "getBackground" function?

  5. #5
    Looks to me like you're trying to do something client side that has no business being done in client side scripting.

    Might I ask why you aren't doing this on the server in something like PHP? You could even store the 'last' image in a session.

    Though are you 100% sure the images are in the same directory as where the script is running? I know that's the obvious thing, but I have to ask.

  6. #6
    Join Date
    Oct 2012
    Posts
    15
    Trying to give a choice of background images from a button click.
    All of the code is above, except for the button
    It displays a random image right now (onclick)
    But sometimes shows a blank screen, a lot.
    Code:
    <input id="background" type="button"  value="Random Background" style="position: absolute;right:0px;top:0px;">
    
    $(document).ready(function(){
    $('#background').click(function(){getBackground();});
    }
    I would much rather display each image from the array in order than random.

    The click function is loaded in (function ready).
    It's not imperative but would be nicer if it worked right.
    If you want to see it... whatarethelotterynumbers.org
    Last edited by Baldy; 06-27-2014 at 07:55 PM.

  7. #7
    Well, you have images that don't exist in that live copy:
    http://whatarethelotterynumbers.org/map.jpg
    http://whatarethelotterynumbers.org/dungeon.jpg

    etc, etc... whole slew of them 404.

    Silly question, you say it works local? Are you sure the filenames are all lower-case? If you're on windows locally it's case insensitive, most servers are case sensitive so if you use all lower-case names in the script, they MUST be all lower-case on the server.

    Side note -- gah that markup... yikes.

  8. #8
    Join Date
    Oct 2012
    Posts
    15
    All of the images load, just not all the time (only locally). Can't figure out why the images aren't showing up online with the image in the URL, they are all there and do work intermittently. Both of the images you listed are in there and do show up.
    I know my code messy but it works (mostly), I'm not a coder, just wanted to make my own site.

  9. #9
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,504
    You would preload the images in background first. Google preloading of images in background and you will find plenty of examples.

    20kb is not a vastly huge file size, If your images are not loading properly when you are loading up the page from the server, you have a bottle neck somewhere, this could be in your home, especially if you are using a WiFi connections and you have other members of the family using WiFi, your router could also be a cause of delay, not enough down stream bandwidth, your router tries to share among users a limited amount of bandwidth. A typical 100Mb line would have a ratio of 60:40 which would mean that your connection would give you 60% of the bandwidth with a 40% headroom for collisions, drops and corrupt data and requests for resending, if you have two people on the same line this plummets further because you will then have two lots of downstream / headroom factors.

    If the bottleneck is in your ISP's line, the bRouter, then the contention ratio may be high as they may be stacking you all in the hopes that you are not all online at the same time, this is felt in download speed drops, I suggest you do random broadband speed tests and not time and speed and plot it on a graph to see if a pattern is forming, you could have someone file sharing. Often a complaint about the speed of your service and pointing out that you are not getting the speed that you are promised in the contract, claiming that they are running a high contention ratio for users on the node you are on will often provoke a settings change and they can give your IP address a higher priority.

    If none of the service is showing up as the problem then the only answer is the server host you are using, likely they are over subscribed and if you are on a shared server, if you have someone that is running a demanding service, that will impact on delivery of your site and data. It takes a complaint to them to get them to investigate why your server service is very slow.
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

  10. #10
    Join Date
    Oct 2012
    Posts
    15
    Thanks for your help.
    I just re-upped my subscription for two years more hosting and troubles started, not big ones but seems funny right after I paid them.
    Thanks again, I'll see what I can do.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles