User:Dnessett/Template/Bref/Clean Back Links.js

From Wikibooks, open books for an open world
Jump to navigation Jump to search
Note: After saving, changes may not occur immediately. Click here to learn how to bypass your browser's cache.
  • Mozilla / Firefox / Safari: hold down Shift while clicking Reload, or press Ctrl-Shift-R (Cmd-Shift-R on Apple Mac);
  • Internet Explorer: hold Ctrl while clicking Refresh, or press Ctrl-F5;
  • Konqueror: simply click the Reload button, or press F5;
  • Opera users may need to completely clear their cache in Tools→Preferences.
// Helper Javascript code that removes backlinks and upper arrows from <references/> formated data.
// Useful for text that calls the Bref template, since in that case backlinks point to display:none text.

  var brefElementNodeType = 1;
  function BrefHideBackLinks()
  {
  // Create array of all Tags with classname "references" and tagName "ol"
  var brefAllReferencesTags = new Array();
  brefAllReferencesTags = getElementsByClassName(document.body, "ol","references");
  //Loop through list elements, blanking text in all child nodes other than <cite> and <span>
  for (var i in brefAllReferencesTags)
    {
    var brefRefList = brefAllReferencesTags[i].childNodes;
    for (var j in brefRefList)
      {
      // Leave embedded text between citation entries alone
      if (brefRefList[j].nodeType == brefElementNodeType)
        {
        var brefRefListEntry = brefRefList[j].childNodes;
        for (var k = brefRefListEntry.length - 1; k >= 0; --k)
          {
          // Only retain the <cite> and <span> tag text. Handle IE as well as other browsers
          if (brefRefListEntry[k].nodeType != brefElementNodeType || (brefRefListEntry[k].tagName != "CITE" && brefRefListEntry[k].tagName != "SPAN"))
            {
            if (brefRefListEntry[k].innerText) // IE inner node
              {
              brefRefListEntry[k].innerText = "";
              }
            else if( brefRefListEntry[k].nodeName && brefRefListEntry[k].nodeName == "#text")
              { // IE list entry text
                brefRefListEntry[k].nodeValue = "";
              }
            else if (brefRefListEntry[k].textContent)
              { // Others
              brefRefListEntry[k].textContent = "";
              }
            }
          }
        }
      }
    }
  }

//
// hook BrefHideBackLinks
//
  addOnloadHook
    (
      function ()
        {
        BrefHideBackLinks();
        }
    );