php - Changing background image src periodically using jQuery and AJAX -
i'm trying have background image of body of page change every (approximatly) n seconds. have php function echoes random image src when call using ajax. replace current body background image src newly generated one, can't work.
here code, there wrong in ?
(function($) { var $body = $('body'); window.setinterval(function() { $.ajax({ url: 'http://localhost/some_path/index.php?exec=getnewbkgimgsrc', }).done(function(data) { var newimage = new image(); newimage.onload = function() { $body.css('background-image', newimage.src); }; newimage.src = data; }); }, 5000); })(window.jquery); and php function work :
public function getnewbkgimgsrc() { echo content_url_dir.'/wallpapers/wallpaper ('.rand(1, 20).').jpg'; } could because i'm using localhost ? can mremeber having ajax problems before when developping locally.
edit : clue maybe style property of body element doesn't sseem change / set in firebug. tried $('body').first() , $(document.body) , using url("") (in value of background-image property) doesn't work either. onload event fire however.
thanks help.
why creating new image object if getting url php? set background-image property of body to:
$body.css('background-image', "url('" + data + "')"); if using vanilla js:
document.body.style.backgroundimage="url('" + data + "')"; see proper format property here: http://www.w3schools.com/cssref/pr_background-image.asp
Comments
Post a Comment