generalize fixLinks to handle #anchors
authorhttps://www.google.com/accounts/o8/id?id=AItOawmK9HFqQh4HBRfrRONxYE22cglDFTfFbaw <John@web>
Wed, 13 Jul 2011 06:03:51 +0000 (02:03 -0400)
committeradmin <admin@branchable.com>
Wed, 13 Jul 2011 06:03:51 +0000 (02:03 -0400)
doc/tips/JavaScript_to_add_index.html_to_file:_links.mdwn

index a11c807e8c7ace028f3a3fba4e33131eb3f36d51..250bb26af4e998a3aeb842cfb7773cd19cca83a6 100644 (file)
@@ -36,4 +36,28 @@ This can be placed in `page.tmpl`:
        ...
        </html>
 
        ...
        </html>
 
-This script has not been extensively tested.
\ No newline at end of file
+This script has not been extensively tested.
+
+---
+
+A version that handles anchors:
+
+
+       function fixLinks() {
+         var scheme = location.protocol;
+         if (scheme != "file:") return;
+         var links = document.getElementsByTagName("a");
+         for (var i = links.length; --i >= 0; ) {
+           var link = links[i];
+           var href = link.href;
+           var anchor = "";
+           var anchorIndex = href.indexOf("#");
+           if (anchorIndex != -1) {
+             anchor = href.substring(anchorIndex);
+             href = href.substring(0, anchorIndex);
+           };
+           var hlen = href.length;
+           if (hlen > 0 && link.protocol==scheme && href.charAt(hlen-1) == "/")
+             links[i].href = href + "index.html" + anchor;
+         }
+       }