views:

374

answers:

2

hi, how could i change images using JavaScript in asp.net? I want these to be changed after some time intervals (say 15 seconds) automatically (without hitting the refresh/F5 button) using java script ONLY in asp.net. Is there any JS which help me to do this.

thanks

+1  A: 

try something like this, this example sets the background image of a page:

var gSlideshowInterval = 10; /* the interval in seconds */
var gImageCapableBrowser;
var gCurrentImage = 0; /* the default image in the array of images */
var randombgs=["images/body/clarinet.jpg", "images/body/jazzsax.jpg", "images/body/composing.jpg", "images/body/classical-music.jpg"];


function canManipulateImages() {
    if (document.images)
     return true;
    else
     return false;
}
function loadSlide(imageURL) {
    document.body.style.backgroundImage='url(\''+ imageURL + '\')';  /* change this line to be the actual image you want to change i.e. document.getElementbyId('MyImage').src or something similar */
    return true;
}
function nextSlide() {
    gCurrentImage = Math.floor(Math.random()*randombgs.length);
    loadSlide(randombgs[gCurrentImage]);
}

gImageCapableBrowser = canManipulateImages();
setInterval("nextSlide()",gSlideshowInterval * 1000);
Mauro
thanks for your prompt reply, but where should i call this JS from?
Romil Nagrani
+1 for adding code
Madi D.
if you add a <script> tag in the head it will auto run itself...you can see the canManipulateImages and set interval arent wrapped in a function and will be called automatically by the browser on load.
Mauro
+1  A: 

the previous answer correct but i should do somthing to it which:

call the method on the body and then

let the function call it self so then you have an infinite loop of rottation

 var gSlideshowInterval = 10; /* the interval in seconds */
 var gImageCapableBrowser;
 var gCurrentImage = 0; /* the default image in the array of images */
 var randombgs=["images/body/clarinet.jpg", "images/body/jazzsax.jpg", "images/body/composing.jpg", "images/body/classical-music.jpg"];

  function canManipulateImages() {
    if (document.images)
        return true;
    else
        return false;
}
function loadSlide(imageURL) {
    document.body.style.backgroundImage='url(\''+ imageURL + '\')';  /* change this line to be the actual image you want to change i.e. document.getElementbyId('MyImage').src or something similar */
    return true;
}
function nextSlide() {
    gCurrentImage = Math.floor(Math.random()*randombgs.length);
    loadSlide(randombgs[gCurrentImage]);

    // the code changed here
    setInterval("nextSlide()",gSlideshowInterval * 1000);

}

 //and this will be call once a page load 
function onBodyLoad()
{
    gImageCapableBrowser = canManipulateImages();
    nextSlide();
}


</script>

and here is the html source

    <body onload="onBodyLoad">
</body>
peacmaker
+1 for automating the actual solution :)
Madi D.