1 [[!meta date="2007-09-10 04:32:55 +0000"]]
3 The source file `foo/bar.mdwn` or `foo/bar.html` generates the
4 page `foo/bar/index.html`, but the links to the page appear
5 as "`foo/bar/`". This is fine (and recommended) for pages
6 served by an http server, but it doesn't work when browsing
7 the pages directly using `file:` URL. The latter might be
8 desirable when testing pages before upload, or if you want to
9 read pages when off-line without access to a web server.
11 Here is a JavaScript "`onload`" script which fixes the URLs
12 if the `local.protocol` isn't `http` or `https`:
15 var scheme = location.protocol;
16 if (scheme=="http:" || scheme=="https:") return;
17 var links = document.getElementsByTagName("a");
18 for (var i = links.length; --i >= 0; ) {
21 var hlen = href.length;
22 if (hlen > 0 && link.protocol==scheme && href.charAt(hlen-1) == "/")
23 links[i].href = href + "index.html";
27 This can be placed in `page.tmpl`:
31 <script language="JavaScript">
37 <body onload="javascript:fixLinks();">
41 This script has not been extensively tested.
45 A version that handles anchors:
49 var scheme = location.protocol;
50 if (scheme != "file:") return;
51 var links = document.getElementsByTagName("a");
52 for (var i = links.length; --i >= 0; ) {
56 var anchorIndex = href.indexOf("#");
57 if (anchorIndex != -1) {
58 anchor = href.substring(anchorIndex);
59 href = href.substring(0, anchorIndex);
61 var hlen = href.length;
62 if (hlen > 0 && link.protocol==scheme && href.charAt(hlen-1) == "/")
63 links[i].href = href + "index.html" + anchor;