Can someone please verify if the code below should work. I don't see any logical mistakes but it's not changing the images based on my screen size. It's only displaying the larger images. I have searched a lot but didnt find any help in preloading images based on screen resolution is this something not possible? The images do load but when i switch my web page from the big monitor to my laptop screen the image doesn't appear fully because it's too big
<html>
<head>
<script type="text/javascript">
var num;
var temp=0;
var speed=9000;
var preloads=[];
var w = screen.width;
if(w <=588) {
preload(
'../images/1.jpg',
'../images/2.jpg',
'../images/3.jpg'
);
}
else if(w>588 && w<=1366)
{
preload(
'../images/1.jpg',
'../images/2.jpg',
'../images/3.jpg'
);
}
else{
preload(
'../images/5.jpg',
'../images/6.jpg',
'../images/7.jpg'
);
}
function preload(){
for(var c=0;c<arguments.length;c++) {
preloads[preloads.length]=new Image();
preloads[preloads.length-1].src=arguments[c];
}
}
function rotateImages() {
num=Math.floor(Math.random()*preloads.length);
if(num==temp){
rotateImages();
}
else {
document.body.style.backgroundImage='url('+preloads[num].src+')';
temp=num;
setTimeout(function(){rotateImages()},speed);
}
}
if(window.addEventListener){
window.addEventListener('load',rotateImages,false);
}
else {
if(window.attachEvent){
window.attachEvent('onload',rotateImages);
}
}
</script>