Motivational Quotes - refresh the page for a new one!

April 17, 2009

Roll Your Own Word Counters

Word counters are a cool way to keep your audience updated on the progress of your manuscript(s). Naturally, in order to keep my audience of one updated (hi mom!), I decided to create my own.

You can get really fancy with these, using custom graphics to create a unique look & feel. I decided to keep mine simple, for two reasons: ease of use and ease of extensibility. Here are the steps to add counters to your own site and customize them.

Note – no coding is required for this. You’ll have to do a bit of HTML tweaking if you want to change what it looks like (color, size, etc), but the code that actually runs this can simply be copy & pasted and left alone.

One other point – I’m using this through Blogger right now, but it can easily be extended for use on your own site or other platforms (WordPress, LiveJournal, etc). You may have to tweak some of these steps to get this to work on another platform, but the concepts remain the same.

How’s It Work?

The file is composed of two distinct sections:

  • JavaScript to calculate the percentage complete and update the visual indicator (aka – the meter) accordingly. This is the code, and does not need to be modified.
  • HTML to display the word count, percentage complete, and the meter, and to format said items. This is the piece you will need to modify to display your manuscript’s title, total word count, current word count, if nothing else.

We’ll look at the HTML first. I’ve highlighted the key items requiring input below – these are the places where you will need to input your manuscript’s name, and the word counts (current and target). Everything else, you can ignore, including the completion percentage (which is calculated for you by the code).

<div id="manuscript" style=" text-align:center; width: 100px;">
<span id="manuscriptTitle">Fantasy Novel</span>
<br />
<div style="background-color:#E8E8E8; width:100px; height:10px; text-align:left;">
<div id="meter" style="background-color:#6666FF; width:0px; height:10px;">&nbsp;</div>
</div>
<div style="width:100px; text-align:center; ">
<span id="currentWordCount">12000</span>
<span> / </span>
<span id="targetWordCount">80000</span>
<br />
<span id="completionPercentage"></span>
</div>
<br />
</div id="manuscript_end">

One other point – it is critical that you don’t modify the names of any of the IDs (manuscripts, currentWordCount, etc) as the code looks for these.

What Do I Do?

Putting these on your site is pretty easy.

If You Are Using Blogger (or any site that allows you to add Gadgets):

  1. Edit your blog Layout
  2. Choose the Page Elements tab (again, assuming Blogger here)
  3. Click ‘Add a Gadget’
  4. Choose HTML/JavaScript
  5. Copy the code (JavaScript & HTML, file is at bottom of this post) and paste into the Blogger gadget window.
  6. Within the gadget window, modify the HTML section to reflect your current manuscript(s).
  7. Save the gadget, and then click the Edit HTML tab on the Layout page.
  8. Locate the <body> tag (best way is simply to search - Ctrl + F). Change to <body onLoad="Load()">, which calls our new JavaScript code.
  9. Save

If You Can Modify the HTML itself (i.e. you have your own site and probably know what you’re doing)

  1. Access your page’s source code
  2. Copy the JavaScript portion of the file below (including the starting <script> tag and the ending </script> tag). Conversely, you can dump this into a separate .js file and just link to your site.
  3. Paste the JavaScript within the <head> of your current page.
  4. Copy the HTML portion of the file below.
  5. Paste the HTML somewhere within your page’s <body>.
  6. Modify the HTML section to reflect your current manuscript(s).
  7. Modify the <body> tag to call the new JavaScript code - <body onLoad="Load()">
  8. Save

Multiple Manuscripts In Progress

If, like me, you have several manuscripts in various forms of completion, adding a second counter (or third, etc) is pretty easy. Just copy and paste another section of HTML, and modify the second occurrence (or third, etc) accordingly.

Modifying

So you’ve got your handy-dandy word counters up and are pretty excited, except they look just like mine. You want to put your own unique stamp on them.

Color

Within the HTML section, locate the background-color attribute. You’ll find two of them – the first is the background of the meter, the color that reflects the words remaining (mine is a light gray). The second occurence of background-color is the color that reflects words written.

You’ll notice that what follows each of these attributes are not color names, but some funky text - #E8E8E8 and #6666FF, respectively. These are the hex values associated with the displayed colors.

Confused? Don’t be. Just refer to this color chart and copy/paste in the hex value of the new color you want.

Text Size

This one is even easier to change. Just look for the attribute called font-size. There are two of them – one for the manuscript title, and one for the word counts and percentage. Default is to 8 pt (which means point-size). Just change the number (upward or downward) to make the text bigger or smaller.

Fatness

Does your word counter need to go on a diet? Look for the height attribute – there’s only one. I prefer a slim meter, so the default is height:10px (pixels). Just like the Text size, changing this number will make your meter grow or shrink.

Length – not so fast

You may notice the width attribute and decide you want to change it from the default value of 100px. Don’t. The code assumes 1 pixel = 1 percentage point. Therefore, if you tweak this value, it will throw off your meter. I may come back and modify the code for this at some point, but for now, it is what it is.

Gimme the Code

JavaScript comes first, followed by the HTML.

<script type="text/javascript">
var completionPercentage = new Array();
var arrayCounter = 0;
function Load()
{
   var collectionDIV = document.getElementsByTagName('div');
   var collectionSPAN = document.getElementsByTagName('span');
   var currentWordCount = 0;
   var targetWordCount = 0;
   var counter = 0;
   //loop for the count & max values and for the percentage field (to set)
   for (var i=0; i < collectionSPAN.length; i++)
   {
       if(collectionSPAN[i].id == 'currentWordCount')
         {
             currentWordCount = collectionSPAN[i].innerHTML;
         }
         else if(collectionSPAN[i].id == 'targetWordCount')
         {
             targetWordCount = collectionSPAN[i].innerHTML;
         }
         else if(collectionSPAN[i].id == 'completionPercentage')
         {
             collectionSPAN[i].innerHTML = CalculateCount(currentWordCount, targetWordCount);
         }
   }

   //loop DIVs collection looking for meters
   for (var i=0; i < collectionDIV.length; i++)
   {      
       if(collectionDIV[i].id == 'meter')
       {
          collectionDIV[i].style.width = completionPercentage[counter] + 'px';
          counter++;
      }
   }                                
}

function CalculateCount(current, target)
{
    //generate percentage
    var percentage = (current / target) * 100;
    //formatting - round to 2 decimal places
    var rounded = Math.round(percentage * 100) / 100;

    //store value to array for later moving the meter
    completionPercentage[arrayCounter] = rounded;
    arrayCounter++;
    //set the text field to the rounded percentage, with formatting applied
    var formatted = "(" + rounded + "%)";
    //return result for displaying
    return formatted;
}
</script>
    
    <div id="manuscript" style="font-size:8pt; text-align:center; width: 100px;">
        <span id="manuscriptTitle">Fantasy Novel</span>
        <br />
        <div style="background-color:#E8E8E8; width:100px; height:10px; text-align:left;">
        <div id="meter" style="background-color:#6666FF; width:0px; height:10px;">&nbsp;</div>
        </div>
        <div style="width:100px; text-align:center; font-size:8pt;">
          <span id="currentWordCount">12000</span>
          <span> / </span>
          <span id="targetWordCount">80000</span>
          <br />
          <span id="completionPercentage"></span>
        </div>
        <br />
    </div id="manuscript_end">

Questions / Issues?

Post here and I’ll get back with you.

April 13, 2009

The Future of Writing

As someone who is working toward publication, the current state of the industry is pretty sobering. Publishers are taking a bath, and the major booksellers are closing stores and laying off employees. I’ve yet to see data on this, but I’m certain publishers are buying less books, if not in general, than specifically from first-time authors.

Against this economic backdrop, you have the push toward the electronic mediums. I’m not going to debate the pros/cons of digital vs paper here, and I don’t think traditional books are going away. But given what is happening to the newspapers, it seems fairly clear that paper is a dying medium. Costs, environmental concerns, and plain old convenience will continue to hoist the digital banners. Blogs, podcasts, eBooks are, if not the future, certainly part of it.

Given all this, what’s an unaccomplished writer to do? How do we fit in?

  1. Don’t worry about what you can’t control. The only thing you can do is write. Keep at it, and know the markets will be there when you’re ready, but…
  2. Don’t count on traditional publishers to be waiting, checkbook open when you finish your opus. Tales abound of professional, full-time writers who can no longer live off writing alone. What’s to say that you, a relative unknown, will be?
  3. Take your career into your own hands. Get your name out there. More, get your work out there. Podcast your novel. Or release it via your blog. One of the growing trends is the publisher pursuing the writer, who already has buzz and a built-in audience. It’s safer for them, and its something I think we’ll continue to see more of.
    1. As a corollary to this, once you have an audience, you can direct-market to them, cutting out the publishing middle man altogether. You’ll reduce your target audience, sure, but you’ll make drastically more money per story.

There’s a lot of uncertainty these days, specifically in the publishing world. You can let these bigger issues paralyze you, or you can figure out how to take advantage of the Internet and make your own destiny.