1 The source file `foo/bar.mdwn` or `foo/bar.html` generates the
2 page `foo/bar/index.html`, but the links to the page appear
3 as "`foo/bar/`". This is fine (and recommended) for pages
4 served by an http server, but it doesn't work when browsing
5 the pages directly using `file:` URL. The latter might be
6 desirable when testing pages before upload, or if you want to
7 read pages when off-line without access to a web server.
9 Here is a JavaScript "`onload`" script which fixes the URLs
10 if the `local.protocol` isn't `http` or `https`:
13 var scheme = location.protocol;
14 if (scheme=="http:" || scheme=="https:") return;
15 var links = document.getElementsByTagName("a");
16 for (var i = links.length; --i >= 0; ) {
19 var hlen = href.length;
20 if (hlen > 0 && link.protocol==scheme && href.charAt(hlen-1) == "/")
21 links[i].href = href + "index.html";
25 This can be placed in `page.tmpl`:
29 <script language="JavaScript">
35 <body onload="javascript:fixLinks();">
39 This script has not been extensively tested.
43 A version that handles anchors:
47 var scheme = location.protocol;
48 if (scheme != "file:") return;
49 var links = document.getElementsByTagName("a");
50 for (var i = links.length; --i >= 0; ) {
54 var anchorIndex = href.indexOf("#");
55 if (anchorIndex != -1) {
56 anchor = href.substring(anchorIndex);
57 href = href.substring(0, anchorIndex);
59 var hlen = href.length;
60 if (hlen > 0 && link.protocol==scheme && href.charAt(hlen-1) == "/")
61 links[i].href = href + "index.html" + anchor;