views:

93

answers:

4

Following are the two ways of calling an image in your webb application.

<img src="/myapp/img/world.gif" />

OR

<img src="http://www.example.com/myapp/img/world.gif" />

Which will be best to use or both have the same meaning. If both do not have the same meaning then why? and is there any performance constraints if I use the second method in my app to call all files (images, swf, flv etc..)

A: 

There is no performance issues but many times you work and test in a development environment and if you use the second form it would imply to change something like this:

<img src="http://beta.example.com/myapp/img/world.gif" />

to

<img src="http://www.example.com/myapp/img/world.gif" />

So it's better to have always an absolute-relative URI for all the resources

victor hugo
I am little bit confused? 1st one is the absolute URL and 2nd type mentioned in ma question is absolute-relative URL. Is it correct?
Prashant
Nope, the first one is the absolute-relative because it's absolute in the context of the actual server but it's relative because it depends on what server are you 'positioned' in. The second one (the longest one) is absolute because it always ALWAYS points to the same place
victor hugo
the first one is an absolute path but a relative URL. The second one is a relative URL. An absolute path will replace any path from the base URI whiel a relative path (like img/world.gif) will be applied to the path component of the base URI
VoidPointer
Any clue about the downvote?
victor hugo
+1  A: 

The former method is a relative URL that locates resources relative to the server’s root. The latter is an absolute URL that indicates not just the directory, but the host, subdomain, even protocol.

They each have their pros and cons. Using a relative path makes it easier to migrate to a new domain since the domain name is not part of the URL. Using an absolute path makes it easier to organize your files since you don’t have to use things like ../../images/ (which can makes things messy and difficult to read).

As for performance, the only issue is that the absolute URLs are a slightly longer (though not always), otherwise no.

Synetech inc.
+6  A: 

Generally speaking, the first method should be your preferred way of referencing any resources that are part of your application. It is called a relative URI reference and it allows you to transfer your application to another domain-name without changing all the links.

You may even consider using relative paths such as

<img src="img/world.gif" />

... assuming that the HTML above appears at some place like http://www.example.com/myapp/main.html

That way you are also not tied to the /myapp path prefix and could easily move your application to /superapp without changing a thing.

Most application frameworks and templating systems have a way of reporting the root URI of the current application. In such cases it may be most convenient to use something like

<img src="$(APPROOT)/img/world.gif" />

... depending on what specific replacement/expansion mechanism your particular envrionment has. Here it is assumed that $(APPROOT) will be replaced with the absolute base URI of the current application.

VoidPointer
A: 

Use your web application framework's HTML helper for generating URLs.

For example,

In Java Servlets: <c:url value='/img/word.gif' />

In CakePHP: <?php echo $html->url('/img/word.gif'); ?>

In Rails:`<%= link_to ... %>

The reason for using your framework's HTML helper is because moving your web application will not disrupt your URLs.

0sumgain