views:

46

answers:

3

I'm attempting to dynamically set the backgroundImage for a div where the image source url is not the true/final destination url but instead returns a 301 redirect pointing to the actual image url. Using this redirecting url in a standard < img src=" ... tag works normally as the browser transparently follows the redirect. However, it apparently does not do the same for backgroundImage.
My only thought at the moment is to use XMLHttpRequest to perform a HEAD request, determine the actual URL from the headers returned and use that as the backgroundImage, but this obviously involves significant overhead. Can anyone offer a better alternative?

+1  A: 

Have a script build the image dynamically based on session or cookie data. I do it all the time.

Azeem.Butt
Depending on the size this can slow down data transfer due to processing.
Ryan Schumacher
+1  A: 

Have your current script that is returning the 301 download/generate the image itself, or use URL rewriting to do the redirect at the server side.

Wim
A: 

If it's a small background image you might consider encoding it in BASE64.

body {
  background-image:url(data:image/png;base64,<base64 STRING>");
}
Ryan Schumacher
data: protocol is not supported in all browsers.
Kevin Peno