How to have Background Image Change on Refresh?

Dear EE experts,

We would like to ask on how to have a banner image that everytime you refresh the site, it'll change its image... we already have that, BUT the problem is, we don't know where to set its size. What we want to happen is even we browse it on a high resolution, the image will stay fit on its cell or box. Because we have a web right now, if we browse it on iphone, the banner is fully fit, even if we rotate it, everything's fine. But when we browse it with iPad, Portrait or Landscape, the size of the image stays the same, but its cell or box is stretched and you can see its bgcolor already...
Is there a way that even a user browse the page using iphone, ipad, or any other tablets, the banner image will fit automatically with its box?

jQuery or CSS or Javascript? Please advise...

Thank you!
Stiebel EltronAsked:
Who is Participating?
 
oheilCommented:
You can get the size of the box with
var height=$("#YourBoxID").height();
var width=$("#YourBoxID").width();
and change the image size with that values.
It would be better to pre-format pictures with different sizes for the different viewers.

Oli
0
 
Stiebel EltronAuthor Commented:
Is it PHP? Oh, sorry but we write our script in ASP...
That's why we couldn't understand your advise Oli...

:(
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
oheilCommented:
No, thats javascript jquery style.
The idea is to adjust the image after the page has loaded using JavaScript and the container has the right size.

Oli
0
 
Stiebel EltronAuthor Commented:
Can you give us an example? Because we really don't know how to start or what to put inside <HEAD>--</HEAD>, etc.

Please help us for a sample...
0
 
Sar1973Commented:
I could propose you to define an ID (or name) of the set of your images like "(URL)/myImage1", "(URL)/myImage2", ... and so on and then apply a Javascript code that loads the background image with a random criteria like this:
var myRandomNMB=Math.floor(Math.random()*10)+1; //if you have 10 images
var newBCKGRDImage=myImage+myRandomNMB;
document.getElementById("myImageBCGRD").style.backgroundImage = newBCKGRDImage;
0
 
oheilCommented:
<html ...>
<head>
...
</head>
<body>
...
<div id="YourBoxID>
<img id="YourImgID" src="some/path/to/image.gif" />
</div>
...
<script type="text/javascript">
<!--
var height=$("#YourBoxID").height();
var width=$("#YourBoxID").width();
$('#YourImgID').width(width);
$('#YourImgID').height(height);
//-->
</script>
</body>
</html>

Open in new window


This is just a beginner example.
A better approach would be to have images available for the possible resolutions you expect. Than you do not change the image size but the image source depending e.g. on the width of the box. But first, you should get the above example running.
If you have problems, put some alerts into it , e.g.:
var height=$("#YourBoxID").height();
var width=$("#YourBoxID").width();
alert(height);
alert(width);
and check if the values make sense.

Oli
0
 
Stiebel EltronAuthor Commented:
Actually, we have a sample of it:
<HEAD>
<SCRIPT LANGUAGE="JavaScript">

<!-- Begin
// Set up the image files to be used.
var theImages = new Array() // do not change this
// To add more image files, continue with the
// pattern below, adding to the array.

theImages[0] = 'IMG/xg-header-00.jpg'
theImages[1] = 'IMG/xg-header-01.jpg'
theImages[2] = 'IMG/xg-header-02.jpg'
theImages[3] = 'IMG/xg-header-03.jpg'
theImages[4] = 'IMG/IL-header.jpg'

// do not edit anything below this line

var j = 0
var p = theImages.length;
var preBuffer = new Array()
for (i = 0; i < p; i++){
   preBuffer[i] = new Image()
   preBuffer[i].src = theImages[i]
}
var whichImage = Math.round(Math.random()*(p-1));
function showImage(){
document.write('<img src="'+theImages[whichImage]+'">');
}

//  End -->
</script>
</HEAD>

<BODY>
<td width="395" valign="middle" bgcolor="#666666">
       	<SCRIPT LANGUAGE="JavaScript">
    	<!-- Begin
		showImage();
		//  End -->
		</script>
    </td>
</BODY>

Open in new window


but the problem is, we don't know where to re-size. Because we want that the image will adjust its size automatically...
0
 
Sar1973Commented:
So I think you need a code that detects onload the page size (or the device type and size) and sets the image size.
0
 
Stiebel EltronAuthor Commented:
@Sar1973, how? Do u have sample?
Or do you have any advise if we'll change / edit the said scripts above?
0
 
Stiebel EltronAuthor Commented:
Great! Got the idea...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.