User:Michaelnelson~enwikibooks/monobook.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.
 function formatBookHeading() {
  // Grab all the h1 elements within the document
  var h1Elements = document.getElementsByTagName('h1');
  
  // We need to check every h1 element in the document:
  for (i = 0; i < h1Elements.length; i++)
  {
    // But we only want to modify the h1 elements that have the
    // class of "firstHeading".
    if (h1Elements[i].className == "firstHeading")
    {
      // Then use the / to split the heading into it's parts
      var headings = h1Elements[i].innerHTML.split('/');
 
  
      //
      // If there is only one heading (ie, the Book Title), then 
      // leave it as the h1 element and do nothing. Otherwise
      // we want to set the Chapter:Subchapter as the h1 heading,
      // and put the book title as a paragraph _before_ the h1
      // heading? (Just check this, I think that's what you're after?
      //
  
      if (headings.length > 1)
      {
      
        // First, create the paragraph that will contain the
        // book title, and give it a class so we can style it from CSS.
        var bookTitle = document.createElement('p');
        bookTitle.setAttribute("class","booktitle");
        
        // Then for the IE bug:
        bookTitle.setAttribute("className","booktitle");
        
        // And add the Booktitle text from the original heading
        bookTitle.innerHTML=headings[0];
        
      
        // Next, set the h1 heading to the Chapter title:
        h1Elements[i].innerHTML = headings[1];
    
        // Check if there's a third sub heading:
        if (headings.length == 3)
        {
          var subchapter = document.createElement('span');
          subchapter.innerHTML = ":" + headings[2];
          
          // and add the subchapter to the h1 heading:
          h1Elements[i].appendChild(subchapter);
        }
        
        // Finally, add the book title before the h1 heading:
        h1Elements[i].parentNode.insertBefore(bookTitle, h1Elements[i]);
        
        // Finally, add the chapter element after the heading.
        //h1Elements[i].parentNode.insertBefore(chapter, h1Elements[i].nextSibling);        
  
      } 
    
 
       
    } // end if class=firstHeading
    
  } //end for loop
   
 }
 
 if (window.addEventListener) window.addEventListener("load",formatBookHeading,false);
 else if (window.attachEvent) window.attachEvent("onload",formatBookHeading);