Unscientific poll: Do you use the <base /> element?

Nov 10 2005

I’ve just started very early work on a new site, and immediately came across a common problem when templating – do you use absolute or relative paths to reference all your CSS, Javascript and image files?

As my working folder was buried down in a series of contract-related directories, absolute paths (i.e. href="/images/myfile.gif") were not an option; but with an eye on the finished product, relative paths (i.e. href="../images/myfile.gif") become a headache later on.

So instead I chose the third way – adding a base element to the head of the document, which sets the starting point for all paths:

<base href="http://localhost/path/to/site" />

With that in place, I could then use absolute paths to all files, safe in the knowledge that even if I move files up or down my folder hierarchy, all the links will still work.

Poll

So I’m interested whether anyone else uses the base element in their site development; I don’t recall seeing it around much, but whether that’s more to do with the prevelance of blogging CMSs nowadays I don’t know.

Over to you.

Filed under: XHTML.

Technorati tags:

Digg this article

Bookmark this article with del.icio.us

Previously: A lyrical plan

Next: My Top Five... Poems


Comments

Adam
1022 days ago
I don’t use the tag, myself. The main reason is because I didn’t know about it. I am using it now, thanks! Just what I was looking for.
#1
Anthony
1021 days ago
Me too, I’d never encountered it until now… but I’ll definately have a look into it!
#2
Matthew Pennell
1021 days ago
Not quite the response I was expecting, but glad I could be of help!

So am I the only person to have even heard of the tag??
#3
Keane
1021 days ago
Nope, I’d never heard of it either. But I have now. How handy. Brilliant, thanks!
#4
Keane
1021 days ago
Actuallly, not as handy as I thought, unfortunately, because it doesn’t seem to act on server-side includes and I use a whole bunch of them. Gah.
#5
Matthew Pennell
1021 days ago
Keane: No, it won’t work at a server-level as you are setting the <base /> href in the client-side (rendered) HTML – any SSIs have already been parsed by that time.

You can do something similar by setting a global variable at the top of each page (perhaps something like this in PHP):

declare(‘ABSPATH’,$_SERVER[“DOCUMENT_ROOT”]);

And then reference that in all your include calls:

<?php include(ABSPATH.'inc/myfile.php'); ?>
#6
C Montoya
1021 days ago
I use:

/image.jpg

This builds the path from my .com root. I like that and it’s easy to work with, very consistent.
#7
Matthew Pennell
1020 days ago
C Montoya: That’s the way I would normally do it, but the trouble starts if you are developing in a folder below the site root. My offline development sites are all located at http://localhost/path/to/site, so using your method would reference files directly below the localhost root – not what I want!

Of course, I could mess around with the hosts file and create a new virtual site for each project, or redirect the actual live domain to 127.0.0.1, but I find using the base tag is one of the easiest ways round the problem. You can still build the path from the root of the site, with the added bonus that you can ‘move’ that root to anywhere you want.
#8
Keane
1018 days ago
Ah, excellent point about the client-side/server-side differentiation. I hadn’t thought of that, but it’s obvious, of course!

The PHP global variable is a sensible workaround—I’ll bear that in mind, cheers.
#9
Matt Vince
1017 days ago
I’ve been using the base tag for many years. It makes path issues much easier to solve.

The best part is when using some sort of server-side processing, you can dynamically generate the base tag and move your site to any folder at all on any server and the paths will remain correct.

I’m glad to see someone else is using this and even more glad that people are learning about it!
#10
Matthew Pennell
1017 days ago
Matt: That’s exactly what I’m doing; my global.php has a switch statement so that the base tag contains the right path, whether it’s on my localhost, my dev site, or the client site.
#11
WD Milner
1012 days ago
I’ve been using it on and off for a very long time. It isn’t always appropriate but it can neatly solve certain problems as you’ve illustated.

I’ve also used it as a troubleshooting tool when diagnosing problems on a client’s page or layout code that I didn’t create. I can capture a copy of their page locally, insert the base reference back to their document root and all the links etc. work properly.
#12
Angie
1012 days ago
I almmost always use the base tag – it speeds up development quite a bit.
#13
Matthew Pennell
1011 days ago
WD: Good point, another excellent use for it – although I’m so used to Firefox’s live CSS editing that I can’t remember the last time I had to save a local copy of a page!
#14
James Hicks
871 days ago
I use it to because mod rewrite in php caused issues in my revertive paths
#15
Alex Taylor
856 days ago
I use it on my sites for the same reason as #15, I use mod_rewrite to point all URL’s to my CMS’s core php file.

The base element makes the code much easier because I just specify a base url and use absolute URL’s from there and I can put the CMS in any folder or sub folder.
#16
Alex
852 days ago
good tip, Thanks, I didn’t know about that tag…
#17