Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript jQuery: Adding background image to div Reply To: jQuery: Adding background image to div

#189185
__
Participant

@htmlcinco, you misunderstand. when you specify a URL in a browser (e.g., the url of an image), that path is resolved in one of several ways:

  1. if the URL starts with a protocol (e.g., “http://”), then it is considered “absolute” and the browser will look up the URL literally. As an example, http://example.com/img/mountain.jpg will always ask example.com for “/img/mountain.jpg”.
  2. otherwise, the URL is considered “relative.” There are many things that might affect how it is resolved, but among other things, the browser will add the protocol, hostname, and/or current path to the URL.

For example,img/mountain.jpg is a relative path; it cannot be resolved on its own. In order to make sense of it, the browser will:
– add the current url path:
/img/mountain.jpg
– add the current hostname:
localhost/img/mountain.jpg
– add the current protocol:
http://localhost/img/mountain.jpg

This is how URLs are resolved. If you don’t want to look for the image on localhost, then you need to specify where to look explicitly.