1 /* Copyright 2005 Alex Schroeder <alex@emacswiki.org>
2 based on http://simon.incutio.com/archive/2004/05/30/plinks#p-13
3 Copyright 2004 Simon Willison
6 function plinkHighlight() {
7 if (/#[0-9]+$/.test(document.location)) {
8 // The user arrived via a plink
9 var plink_id = document.location.href.split('#')[1];
10 var para = document.getElementById("p" + plink_id);
11 para.className = 'plink';
15 function addpLinks() {
16 /* Only show plinks on ordinary pages: They either use path_info
17 * or keywords in the URL, not parameters. */
18 if (/=/.test(document.location.href)) {
21 // find the content div: why is there no xpath?
22 var elem = document.getElementsByTagName('div');
25 for (var i = 0; i < elem.length; i++) {
26 var classes = elem[i].getAttribute('class').split(" ");
27 for (var j = 0; j < classes.length; j++) {
28 if (classes[j] == 'content') {
34 // identify all p and li items
35 var items = new Array;
36 elem = content.getElementsByTagName('p');
37 for (var i = 0; i < elem.length; i++) {
40 elem = content.getElementsByTagName('li');
41 for (var i = 0; i < elem.length; i++) {
44 // add named anchors at the beginning and a link to that anchor at
45 // the end of all items
46 for (var i = 0; i < items.length; i++) {
47 var current = items[i];
48 current.setAttribute("id", "p" + i);
49 var anchor = document.createElement('a');
51 current.insertBefore(anchor, current.firstChild);
52 var plink = document.createElement('a');
53 plink.href = document.location.href.split('#')[0] + '#' + i;
54 plink.className = 'plink';
55 plink.appendChild(document.createTextNode(' #'));
56 current.appendChild(plink);
60 function addLoadEvent(func) {
61 var oldonload = window.onload;
62 if (typeof window.onload != 'function') {
65 window.onload = function() {
72 addLoadEvent(addpLinks);
73 addLoadEvent(plinkHighlight);