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?
views:
46answers:
3
+1
A:
Have a script build the image dynamically based on session or cookie data. I do it all the time.
Azeem.Butt
2009-10-30 16:17:02
Depending on the size this can slow down data transfer due to processing.
Ryan Schumacher
2009-10-30 16:31:39
+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
2009-10-30 16:19:36
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
2009-10-30 16:34:05