Jump to content

Recommended Posts

Posted

Hey all,

 

I was wondering if there's a simple way to update a div background without needing a page reload. I know javascript/ajax can do it for a normal image, but not seen a way to do it with divs as such.

 

 

Looking for the div to reload the image every X seconds if possible?

 

Sure I'm missing something obvious here though :)

 

Thanks,

Steve

Posted (edited)

you should have a look at the setTimeout function...and maybe use jQuery as this makes the whole thing much simpler.

    

<br />
      $('document').ready(function(){<br />
        setTimeout("imgSwap()",500);<br />
      });  <br />
      function imgSwap(){<br />
        $('#test_div').css('background-image','url(desktop2.jpg)');<br />
      }<br />


Edited by CESIL
  • Thanks 1
Posted
you should have a look at the setTimeout function...and maybe use jQuery as this makes the whole thing much simpler.

    

<br />
      $('document').ready(function(){<br />
        setTimeout("imgSwap()",500);<br />
      });  <br />
      function imgSwap(){<br />
        $('#test_div').css('background-image','url(desktop2.jpg)');<br />
      }<br />


 

Thanks, I'm assuming to make it an infinite loop you'd just add the whole thing to a function, then recall the function at the end?

 

Thanks,

Steve

Posted

The main function will run after the page is loaded and then the timeout will fire every x milliseconds until cancelled. I have corrected a couple of typos...


      $('document').ready(function(){        t = setTimeout("imgSwap()",500);      });        function imgSwap(){        $('#test_div').css('background-image','url(desktop2.jpg)');      });

@mac_shinobi the OP stated that he didn't want a page reload :)

  • Thanks 2
Posted
The main function will run after the page is loaded and then the timeout will fire every x milliseconds until cancelled. I have corrected a couple of typos...


      $('document').ready(function(){        t = setTimeout("imgSwap()",500);      });        function imgSwap(){        $('#test_div').css('background-image','url(desktop2.jpg)');      });

@mac_shinobi the OP stated that he didn't want a page reload :)

 

What does the JQuery stuff do and how do you install or use that ?

Posted
The main function will run after the page is loaded and then the timeout will fire every x milliseconds until cancelled. I have corrected a couple of typos...

 

Ah lovely :) Will give it a shot tonight. Thanks!

 

What does the JQuery stuff do and how do you install or use that ?

 

It's basically a javascript library that has lots of prewritten functions, just makes life easier :)

 

Steve

Posted
Ah lovely :) Will give it a shot tonight. Thanks!

 

 

 

It's basically a javascript library that has lots of prewritten functions, just makes life easier :)

 

Steve

 

which functions in the example @CESIL posted were from JQuery ?

Posted

Sorry, I should have said that you need to either download jQuery or link an online source of jQuery.

 

mine is in a js directory...

 

I have realised I posted a broken version :doh:

 

Here is a working version

    
       
       
       
       <br />
            $('document').ready(function(){<br />
                setTimeout("imgSwap()",2000);<br />
            });  <br />
            function imgSwap(){<br />
                alert("swapped");<br />
                $('#test_div').css('background-image','url(desktop2.jpg)');<br />
                setTimeout("imgSwap()",2000);<br />
            }<br />
        
   
   

 

The alert is just there to prove the timer is working.

  • Thanks 2
Posted

So you've got an image that gets overwritten each X seconds and you want to force a browser to update that element each X seconds without a page refresh?

 

Have you pieced together a satisfactory solution from the replies so far, or still looking?

 

Cheers,

Jann

Posted
So you've got an image that gets overwritten each X seconds and you want to force a browser to update that element each X seconds without a page refresh?

 

Have you pieced together a satisfactory solution from the replies so far, or still looking?

 

Cheers,

Jann

 

Aye that's the ticket, the jquery bit is working fine currently, only issue I'm still working on is being named the same they're adding into cache, and even using a HTTP no cache header doesn't seem to fix it.

 

Thanks,

Steve

Posted
Aye that's the ticket, the jquery bit is working fine currently, only issue I'm still working on is being named the same they're adding into cache, and even using a HTTP no cache header doesn't seem to fix it.

 

Thanks,

Steve

Try adding the current time using Javascript Date object method .getTime() to the end of the url ie

var d = new Date();
$fName = 'url("desktop.jpg?'+d.getTime()+'")';

  • Thanks 1
Posted
Try adding the current time using Javascript Date object method .getTime() to the end of the url ie

var d = new Date();
$fName = 'url("desktop.jpg?'+d.getTime()+'")';

 

Will do, Thanks!

 

Steve

  • 2 years later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...