PDA

View Full Version : Wow!



duncanhall
March 6th, 2007, 12:07 PM
I've just found an HTML tag I never knew existed, and it's something I've wished I had many times before!

The <base href=""> tag lets you set a default prefix to all links to external sources.

So for example:



<html>
<head>
<base href="http://www.kirupa.com">
</head>

<body>
<a href="web/index.htm">PHP</a><br />
<a href="forum">FORUM</a><br />
</body>
</html>
The two links above would point to "http://www.kirupa.com/web/index.htm" and "http://www.kirupa.com/forum" respectively.

You can even set the base href as something like "http://mydomain.com/folder/subFolder" and then traverse back up the directory tree using ".../" in your links.

I can see how this probably sounds very dull, but I think it's brilliant, especially as I've never heard of it before.

http://www.w3.org/TR/html401/struct/links.html#edef-BASE

fasterthanlight™
March 6th, 2007, 12:29 PM
I can safely say I too haven't stumbled across this tag until now.
:beer:

duncanhall
March 6th, 2007, 12:40 PM
Excellent - that means you owe me a tag! There could be a whole world of undiscovered page elements out there, just waiting to be manipulated and ultimately let down by internet explorer.

Zaid_W1red
March 6th, 2007, 12:49 PM
Gawd bless choo :):lol:

fasterthanlight™
March 6th, 2007, 12:57 PM
but wait, theres no point if you're not linking outside of your site,

because

<a href="/poo.html">Poo</a>

automatically assumes its within the domain the code is residing on........

ramie
March 6th, 2007, 01:06 PM
using an absolute url is good for search engines because there is no room for ambiguity, if a spider can't index a url properly it can go into a loop or all types of idiotic errors occur, rare but it happens, there are lots of other reasons, mostly SEO related, google it if you can be bothered.

duncanhall
March 6th, 2007, 01:12 PM
Yes, obviously for day to day web development you'd have to be a little bit mental to be using the <base href> tag on every page you made.

But the kirupa forums offer a good example of when it definately is useful. For instance, say johnny noobFace comes along and says "Hey, I'm lame at CSS and need someone to help me. Nothing's working. Go to http://website.com/page and see what I mean. Can someone help me?".

Being the digital superheroes we are, of course we rush to his aid, and see that some html/css fiddling is needed. You copy his source code and make a test version on your machine, but lo and behold, none of the images work!

Now, thanks to my friend, <base href="">, one simple line of code causes all external links to start working again.

:party:

ramie
March 6th, 2007, 01:17 PM
Yes, obviously for day to day web development you'd have to be a little bit mental to be using the <base href> tag on every page you made.

lol you obviously read and digested my post; again its good for SEO so its not that mental, possibly more mental you dismiss it without really knowing the implications of absolute vs relative URL's

fasterthanlight™
March 6th, 2007, 01:42 PM
Yes, obviously for day to day web development you'd have to be a little bit mental to be using the <base href> tag on every page you made.

But the kirupa forums offer a good example of when it definately is useful. For instance, say johnny noobFace comes along and says "Hey, I'm lame at CSS and need someone to help me. Nothing's working. Go to http://website.com/page and see what I mean. Can someone help me?".

Being the digital superheroes we are, of course we rush to his aid, and see that some html/css fiddling is needed. You copy his source code and make a test version on your machine, but lo and behold, none of the images work!

Now, thanks to my friend, <base href="">, one simple line of code causes all external links to start working again.

:party:


well sure, once you go in and change all of his absolute URLs to relative if they aren't

foodpk
March 6th, 2007, 05:30 PM
I recon all you who say that there's no point if you're not linking outside your site should try doing some Apache mod_rewriting without using the base tag. Mod rewriting is reading URLs like http://yoursite.com/foo/bar/zork/ and turning them into stuff like index.php?a=foo&b=bar&c=zork. You do mod rewriting when you want these clean URLs you see on some sites.

But the problem is that if you use that, the page thinks it's in /foo/bar/zork/ and not in / where it actually resides, so you have to adjust all your images, links etc. to that. And sometimes you may have an extra 'fake directory' (like /foo/bar/zork/narf/) and what are you gonna do then?

Well then you have to use the base tag and it solves quite a few of your problems. But not all of them, because mod rewriting is harder than writing a HTTP client in assembler ... blindfolded.

Also, good news, the base tag has been supported since IE3, so you probably won't run into any compatibility issues using it.

:afro: