]> sipb.mit.edu Git - ikiwiki.git/blob - doc/tips/JavaScript_to_add_index.html_to_file:_links.mdwn
add discussion on this tip
[ikiwiki.git] / doc / tips / JavaScript_to_add_index.html_to_file:_links.mdwn
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.
8
9 Here is a JavaScript "`onload`" script which fixes the URLs
10 if the `local.protocol` isn't `http` or `https`:
11
12         function fixLinks() {
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; ) {
17            var link = links[i];
18             var href = link.href;
19             var hlen = href.length;
20            if (hlen > 0 && link.protocol==scheme && href.charAt(hlen-1) == "/")
21              links[i].href = href + "index.html";
22           }
23         }
24
25 This can be placed in `page.tmpl`:
26
27         <html>
28         <head>
29         <script language="JavaScript">
30         function fixLinks() {
31         ...
32         }
33         </script>
34         </head>
35         <body onload="javascript:fixLinks();">
36         ...
37         </html>
38
39 This script has not been extensively tested.