18

I'm working on an HTML5 mobile app and I initially have the background of a DIV item set through the CSS as follows:

background-image: url('images/ClanSpider.png');

In my app, I have a method that changes the background DIV based on a selection made in a dropdown list from a previous method using jQuery:

function ResetMyHonor()
{       
   ClanImage = 'images/Clan' + MyClanName + '.png';
   $("#MyClanName").html(MyClanName); 
   $("#MyHonorBox").css('backgroundImage', 'url(' + ClanImage + ')');
}

All of this works fine when I'm on the root of my page. However, I have some links within the app using hash tags to navigate the page (such as #MyHonor). When I've navigated to one of these tags and call my reset function above, the image breaks. When I pull up the Chrome Inspector to look at the DIV tag, it says that the image it is trying to load is "images/MyHonor/ClanSpider.png" which doesn't exist.

I know the CSS url will generate links in reference to its location within the application, but it doesn't matter where I move the CSS files in the application.

Is there a way for me to rewrite what comes out of the url processing or an alternate way of specifying the background image of the DIV without doing any kind of server side processing? Ideally this app will run through the manifest cache feature of HTML5, so I won't have access to any server based languages.

ArK
  • 20,698
  • 67
  • 109
  • 136
Dillie-O
  • 29,277
  • 14
  • 101
  • 140

5 Answers5

16

Try putting a leading slash on those paths to represent the root.

ie use:

url('/images/ClanSpider.png')

instead of

url('images/ClanSpider.png')
Andrew M
  • 9,149
  • 6
  • 44
  • 63
  • I have tried this, but it then defaults to the root of the server. When developing locally, I lose my project path and have file:///H:/images/ClanSpider.png when it should be file:///H:/Projects/L5RHonor/images/ClanSpider.png. I realize that sounds a bit touchy, but if the application manifest functions similarly, I'd lose the images as well. – Dillie-O Nov 15 '10 at 13:43
  • @Dillie-O - are you using a web server locally to develop with, or developing the files on the disk and loading them directly into a browser? What will be the root of the website once it's on a real live server? – Andrew M Nov 15 '10 at 14:00
  • A little bit of both. I do local development and just test via Chrome/Safari for core functionality. I then push it up to a server where it isn't in any subfolder. Current location is http://counter.roku-mart.com – Dillie-O Nov 15 '10 at 14:10
  • 1
    When it's on the server do you have the same problem? Like you touched on in your comment, paths starting "/" are relative to the website root, which is usually what you want, just fully qualify them from there. Unfortunately if browsing straight from the file system the "root" will be in a different (and maybe inconsistent) place. If the problem is only local, it might be necesarry to install Apache or something similar, or move all of your working files into the root of your disk (ugh). – Andrew M Nov 15 '10 at 14:39
  • url('images/ClanSpider.png'), this code is working. Thanks. – Mohammad Jahangeer Ansari Oct 03 '20 at 11:27
10

From reading through your comments on the other answers I think you're creating a problem for yourself that doesn't really exist. If url('/images/ClanSpider.png') is going to work when you upload to the web server then the trick is to make it work the same way when working locally. By far the easiest way to do this, especially if your focus is an offline app which has little in the way of server side requirements (which I'm assuming is true, as you mentioned file:/// URIs), is to run a local web server.

Python ships with a module SimpleHTTPServer, if you have Python installed then starting it is as simple as going to your L5RHonor project directory in a command prompt and issuing the following command:

python -m SimpleHTTPServer

Then instead of accessing your files with URIs like this:

file:///H:/Projects/L5RHonor/images/ClanSpider.png

You will access them like this:

http://localhost:8000/images/ClanSpider.png

All your root relative file paths will now work correctly, as an added bonus the offline caching will work correctly in Chrome and you'll be able to see from the log in the command prompt window that it is requesting and caching the correct files according to your manifest.

robertc
  • 74,533
  • 18
  • 193
  • 177
  • 1
    Good explanation about creating my own problems. Going back to the "standard" method and firing things up through Visual Studio dev server (in my case) helped me finish things off. – Dillie-O Nov 16 '10 at 19:56
4

The simplest solution is obviously adding a slash to the URL to make it absolute. That will work fine, but it makes it impossible to move the application into a sub-directory, or to move static resources to a different server. If that is a problem, there are various alternative ways.

If the number of possible background images is finite, you could define every one in a class of its own:

.bgSpider { background-image: url('images/ClanSpider.png'); }
.bgFalcon { background-image: url('images/ClanFalcon.png'); } 
...

and then do an .addClass() to set the correct image.

Other than that, as far as I know, there is no way to specify a path relative to the style sheet (rather than the current document) when setting a background image path in Javascript. You would have to work with absolute paths, or define a root path in JavaScript, and use that:

// in the script head
imageRoot  = "http://www.example.com/mysite/images";
// later....
$("#MyHonorBox").css('backgroundImage', 'url(' + imageRoot + ClanImage + ')');
Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • There is a finite set of background images, so I do like that option. Do you know if defining a root path with the full URL will still work when the application manifest pulls down the application to work with? All the documentation I've seen involves relative paths. – Dillie-O Nov 15 '10 at 13:39
  • @Dillie I'm not familiar with HTML 5 mobile apps so I can't answer that for sure! Good question. It's well possible you can use absolute URLs and they get cached when pulled down though. – Pekka Nov 15 '10 at 13:49
  • Thanks for the help, but this route wound up not working for me. I think maybe it was due to the translation of #MyHonor/images/image.png to /MyHonor/images/image.png. – Dillie-O Nov 16 '10 at 19:55
1

The location of the CSS file is irrelevant, you are modifying the .style property of an HTML element. This is the same as using the style attribute.

As this is CSS embedded in the document, all URIs are relative to the document.

You probably want to start the URL with a /, or if you really want the absolute location specified in your question: http://

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

Try adding a / at the start of the URL?

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592