1 of 2
1
Body background image repainting on every page reload in IE7
Posted: 04 June 2008 02:49 PM   [ Ignore ]
Newbie
Rank
Total Posts:  1
Joined  2008-06-04

The site I am working on uses a bg image in the body element style in the css file. I notice as I navigate around the site with IE 7 that the image appears to reload as I move around the site.

As I investigated further I discovered that the image is not actually reloading (I looked at the server logs). Rather it appears that IE 7 is pulling the image from the browser cache but is repainting the image on every page request. Either way, the image is the last thing to be drawn on the page and consequently the background “pops” each time the user loads a page with the same background. It’s really annoying. FF does not do this nor does Safari on the Mac.

The site is here: http://revfoods.com

Here’s the CSS snippet that would affect this:

body {
margin: 0;
padding: 0;
background: url(/images/application/background-image.jpg) no-repeat top center;
text-align: center
}

I tried substituting the jpg for a png but no dice. Any ideas?

Profile
 
 
Posted: 05 June 2008 01:36 PM   [ Ignore ]   [ # 1 ]
Jr. Member
RankRank
Total Posts:  38
Joined  2007-09-03

Dylan,

The problem you have run into is a very well known one.  Don’t bother playing around with PNG, JPG, GIF etc - it will not make a blind bit of difference. Given below is the explanation to the problem and then the solution.  If you can’t be bothered understanding the explanation just skip to the solution.


Explanation

IE, as is rather well known, is particularly dumb.  When it encounters an image tag it does the following

1. checks to see if the image is available in its local cache.
2. Should the image not be available it, quite reasonably, gets it from the server in question.  The request it sends goes like this

GET /scripts/justlogo.png HTTP/1.1
Referer
http://localhost/btest.html
Accept-Languageen-us,en-gb;q=0.5
Accept
-Encodinggzipdeflate
User
-AgentMozilla/4.0 (compatibleMSIE 6.0Windows NT 5.1SV1Mozilla/4.0 (compatibleMSIE 6.0Windows NT 5.1SV1) ; .NET CLR 2.0.50727)
Hostlocalhost
Connection
Keep-Alive 

3. Should the image be available it checks to see when it was downloaded and then sends of a request to the server anyway.  Here is that second request

GET /scripts/justlogo.png HTTP/1.1
Referer
http://localhost/ctest.html
Accept-Languageen-us,en-gb;q=0.5
Accept
-Encodinggzipdeflate
If-Modified-SinceThu05 Jun 2008 10:44:37 GMT
User
-AgentMozilla/4.0 (compatibleMSIE 6.0Windows NT 5.1SV1Mozilla/4.0 (compatibleMSIE 6.0Windows NT 5.1SV1) ; .NET CLR 2.0.50727)
Hostlocalhost
Connection
Keep-Alive 

The important bit here is If-Modified-Since:.  The server - quite resonably - responds saying that the image has not been modified since the date in question.

HTTP/1.1 304 Not Modified
Content
-Typetext/html
Content
-Length
ConnectionKeep-Alive
Keep
-Alivetimeout=30000max=15
Date
Thu05 Jun 2008 14:04:24 GMT
Server
Abyss/2.4.0.3-X1-Win32 AbyssLib/2.4.0.3
Content
-Length

The important bit here is 304 Not Modified which persuades IE to use the cached image.  The trouble is that this wholly unecessary round trip to the server causes a perceptible delay.  Other browsers - Firefox, Opera, Safari… - do make more logical assumptions and do a better job. How?  They check the Last Modified value from the header sent with the original (first) image request

HTTP/1.1 200 OK
Content
-Typeimage/png
Content
-Length8346
Last
-ModifiedThu05 Jun 2008 10:44:37 GMT
Connection
Keep-Alive
Keep
-Alivetimeout=30000max=15
Date
Thu05 Jun 2008 14:04:18 GMT
Server
Abyss/2.4.0.3-X1-Win32 AbyssLib/2.4.0.3 

and make a decision as to whether they should attempt to download the image again.

Solution

IE users can deal with this problem by adjusting their browser settings.  Needless to say, you cannot rely on that.  It is possible to persuade IE to behave using a small bit of JavaScript that instructs it to Use that Cache!.  However, this may not always work

a. The user is using IE5.5 or earlier, or IE6 without Windows XP Service Pack 1 (unlikely but not impossible) or

b. JavaScript has been turned off.

In my experience both possibilities are somewhat theoretical - none of my sites have any significant traffic that uses IE6- and less than 3% of visitors have JavaScript turned off.  Nevertheless it is best not to make any client side assumptions and use a server-side solution instead.  The steps involved are these

a. Modify your image url. For instance instead of

body{background-image:url(image.png)
make it
body{background-image:url(image.php)

b. Then write this very simple PHP script

<?php
header
("Expires: Fri, 31 Dec 2010 23:59:00 GMT");
header("Content-type: image/png");
$img = @imagecreatefrompng('image.png');
imagepng($img);
?> 

What this does is simple

a. We inject a header into the response from the script to indicate when the contents being squirted down expire. I have put in a date at random.  Note that you need to add these custom headers before you do anything else.
b. Specify the content type - could be different in your case.
c. Load the image.  If your image is not located in the same directory as your script (it should not be) you will have to make sure you specify the path to it correctly.
d. Squirt the image back to the browser in question

That is it!  This way you don’t end up with any ugly IE-specific JavaScript code, don’t have to worry about which IE the user is using, whether JavaScript is turned on or not… .

In case you are curious it is well worth examining the requests sent out by IE to see just what happens under the covers.  Here is a freebie Freeware HTTP Packet Sniffer.

Hope this helps.

Profile
 
 
Posted: 06 June 2008 01:37 PM   [ Ignore ]   [ # 2 ]
Jr. Member
RankRank
Total Posts:  38
Joined  2007-09-03

There are two reliable server side fixes to this problem - one using .htaccess and the other using a PHP script as described in the previous post. Both are available here

IE Flicker Fix

Profile
 
 
Posted: 10 June 2008 04:06 PM   [ Ignore ]   [ # 3 ]
Newbie
Rank
Total Posts:  4
Joined  2008-06-10

I tried the way you guys have suggested in the above, but it wont work.  I have a Main folder which contains for all the site pages, then CSS folder and Image folder. In the css file I put

body{
        background
-color:#ffffff;
    
text-align:left;
    
background-imageurl(background.php);
     
background-positioncenter top;
     
background-repeatno-repeat;
     
height900px;
     
padding0;
    

and I put the background.php in the CSS folder and the

<?php
header
("Expires: Fri, 31 Dec 2010 23:59:00 GMT");
header("Content-type: image/jpg");
$img = @imagecreatefromjpeg('images/background.jpg');
imagejpeg($img);
?> 
Profile
 
 
Posted: 11 June 2008 04:48 AM   [ Ignore ]   [ # 4 ]
Jr. Member
RankRank
Total Posts:  38
Joined  2007-09-03

Could you be more specific as to what did not work? Did IE keep reloading the image or did you get no image at all?  If the latter it could be that the path you are providing to the JPEG file is incorrect/incomplete. For instance, you might need /images/ rather than images/.

If it is neither of these two things the best thing you can do at this point is start by examining the request and response headers to see just what is going on. There are various freebies available for this purpose.  However, I have found that they tend to fall short of providing the range of features one needs of such a product.  There is a commercial alternative called IEInspector that works very well.  You can try it out for free for a month.

In any case, look at the link in the third post in this thread.  If you have access to .htaccess on your server that is the best possible solution.  In case you post back with further questions, just provide the offending URL in the post.

Profile
 
 
Posted: 11 June 2008 04:08 PM   [ Ignore ]   [ # 5 ]
Newbie
Rank
Total Posts:  4
Joined  2008-06-10

sorry for not being clear.
1. Images show up on my share hosting server, but it wont show up on my local development Laptop even though both are using the same files.
2. I tested using IE 7 on vista for this link http://www.ahzhou.com/tests and still gets the reload, blink or flick effect. Also, I checked the background images were cached and the status line says response from cache using IEwatch.

3. As for the .htaccess file, after you added those codes onto the file, do you have to restart your server?


Thank you

Profile
 
 
Posted: 12 June 2008 08:49 AM   [ Ignore ]   [ # 6 ]
Jr. Member
RankRank
Total Posts:  38
Joined  2007-09-03

A demo of IE flicker and the two fixes discussed here - IE Flicker Fix - can be found here. The reason you are still seeing a flicker has to do with the fact that IE’s failure to use cached information extends beyond image files.  In your case, the background image is being specified in an external stylesheet.  Guess what IE does when it finds that external stylesheet link?  Right!  It goes and checks the server for a more recent version!  Use an .htaccess fix on this stylesheet and things should be better.

Modifications to .htaccess come into effect immediately - i.e. no need to restart the server.

Profile
 
 
Posted: 12 June 2008 02:53 PM   [ Ignore ]   [ # 7 ]
Newbie
Rank
Total Posts:  4
Joined  2008-06-10

Thank you for the explanation! But how do I Use an .htaccess to fix on this stylesheet ? What do I have to put in the .htaccess?

Thanks

Profile
 
 
Posted: 12 June 2008 04:09 PM   [ Ignore ]   [ # 8 ]
Jr. Member
RankRank
Total Posts:  38
Joined  2007-09-03

The file is pretty much the same as the one for images.  It would go something like this

ExpiresActive On
ExpiresByType text
/css A2592000 

Make a text file using Notepad or something with the above in it and save it as .htaccess.  That is the name, not the extension.  In Notepad you can do this by changing the “Save As Type” to “All Files”.

Needless to say, if you are serving up other kinds of files from that same directory and you don’t want IE to keep checking for updates then you would need to add an entry for each such file type.  For instance

ExpiresByType text/javascript A2592000

to stop .js files from being checked each time they are encountered.

Profile
 
 
Posted: 16 June 2008 05:36 PM   [ Ignore ]   [ # 9 ]
Newbie
Rank
Total Posts:  4
Joined  2008-06-10

I put the .htaccess file in the root folder and still it doesnt work. Do I have to put it in the same folder as the website files?
do I have to load the modle_ expires?

Profile
 
 
Posted: 12 December 2008 04:13 AM   [ Ignore ]   [ # 10 ]
Newbie
Rank
Total Posts:  2
Joined  2008-12-12

I am having the same issue with my site http://www.creativechildrenscenter.mozakdesign.com. Have you had any success finding a solution. Mine is perfect in FireFox but IE7 is driving me nuts

Profile
 
 
Posted: 19 December 2008 05:10 AM   [ Ignore ]   [ # 11 ]
Newbie
Rank
Total Posts:  3
Joined  2008-12-19

it is neither of these two things the best thing you can do at this point is start by examining the request and response headers to see just what is going on. There are various freebies available for this purpose.

Profile
 
 
Posted: 17 August 2009 03:35 PM   [ Ignore ]   [ # 12 ]
Newbie
Rank
Total Posts:  1
Joined  2009-08-17

I tried the .htaccess approach but it showed no effect. What might be the reason? in my drupal installation there was already an .htaccess in place so I added the lines you provided unchanged to the bottom of the file. The .htaccess file is not in the images folder but in the drupal root folder. Does this need to be elsewhere? Thanks in advance.

Profile
 
 
Posted: 20 August 2009 03:04 AM   [ Ignore ]   [ # 13 ]
Newbie
Avatar
Rank
Total Posts:  1
Joined  2009-08-16

Sooo clearly. Thank you guys. I love this forum.

http://www.ifsneaker.com

Profile
 
 
Posted: 06 October 2009 10:23 AM   [ Ignore ]   [ # 14 ]
Newbie
Rank
Total Posts:  1
Joined  2009-10-06

There are two reliable server side fixes to this problem - one using .htaccess and the other using a PHP script as described in the previous post. Both are available here

Regards

Justine

___
dossier surendettement

Profile
 
 
Posted: 19 July 2010 10:45 AM   [ Ignore ]   [ # 15 ]
Newbie
Avatar
Rank
Total Posts:  9
Joined  2010-07-19

Great stuff. I wish i’d have found this forum a few months ago lol

Profile
 
 
   
1 of 2
1