4

In chrome, safari, and opera setting the background image to an absolute reference such as: "/images/image.png" changes it to "http://sitepath/images/image.png".

It does not do this in firefox.

Is there any way to avoid this behavior, or is it written into the browser's javascript engine? Using jquery to set the background-image also does this problem.

The problem is that I am posting the HTML to a php script that needs the urls in this specific format. I know that setting the image path relative fixes this, but I can't do that.

The only other alternative would be to use a regexp. to convert the urls.

Thanks.

Test this in firefox, and chrome / webkit browser:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<div style="height:400px;width:400px;background-image:url(/images/images/logo.gif);">
</div>
<br />
<br />
<div id="test" style="height:400px;width:400px;">
</div>
<script type="text/javascript" src="/javascripts/jquery.js"></script>
<script type="text/javascript">
    $(document).ready(function(){
        $("#test").css('background-image',"url(/images/images/logo.gif)");
        alert(document.getElementById('test').style.backgroundImage);
    });
</script>
</body>
</html>
1
  • 1
    Personally, I wouldn't try to rely on something like this in the first place. I tried 4 browsers, and all of them give a slightly different result. Commented May 1, 2010 at 16:46

3 Answers 3

3

Not sure exacly how you're putting that location into the document, but you can use window.location.hostname to get the domain and prepend that.

var bgImg = window.location.hostname + '/images/images/logo.gif';
$("#test").css('background-image', 'url('+bgImg+')');

You would replace /images/images/logo.gif with however you generate the image (server-side, I guess?)

Sign up to request clarification or add additional context in comments.

Comments

2

A better approach would be to change classes on the item such that the new class changed the image to whatever you wanted. Something like:

$("#test").addClass("newClass");

This is a much cleaner approach that will degrade better and allow proper separation of concerns.

If you have to stick with changing inline CSS, you'll have to use an absolute reference to keep it the same in all browsers.

$("#test").css("background", "url(" + window.location.hostname + "/logo.gif)");

1 Comment

This is really the better solution, as it not only avoids issues with relative and absolute paths but also gives a better separation of concerns.
-1
$('#divID').css("background-image", "url(/myimage.jpg)");  

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.