The Hickensian

30.12.03 Live Comment Previews

Hope you all had a good Christmas! In response to the most queries I’ve ever received about the site (_both_ of Hicksdesign’s readers asked about this one!), here’s an explanation of the live comment previews I’ve started using.

While looking for a solution to MovableType’s cgi-only comment preview pages (cgi means no php includes) I discovered an alternative – live comment previews. An article at Scriptygoddess, explained how javascript can be used to write form information to a <div>, refreshing everytime a key is pressed. It’s pretty basic, no line breaks of html previews, just the raw text, but the <div> could reuse the same CSS as your comments. Leading on from this, there was an enhanced version posted at Glimpse of a Girl which allowed line breaks to be shown.

There are 2 pieces of form information that I needed previewing – the author’s name and the comment body. As author names won’t need line breaks, I’ve used the simpler version for former, and the enhanced version for the latter. Here’s the complete set, with the <id> changed to suit Movable Type’s defaults of ‘text’ and ‘author’.

// live comment preview  - comment body
function ReloadTextDiv()
{
    var NewText = document.getElementById("text").value;
    splitText = NewText.split(/\n/);
    var DivElement = document.getElementById("TextDisplay");
    var i = 0;
    DivElement.innerHTML = '';
    for(i = 0; i < splitText.length; i++) {
      if(splitText[i].length > 0 ) {
        DivElement.innerHTML += splitText[i] + "<br />";
      }
    }
}
// live comment preview - author
function ReloadNameDiv()
{
var NewText = document.getElementById("author").value;
var DivElement = document.getElementById("NameDisplay");
DivElement.innerHTML = NewText;
}

Then the onkeyup events were added to the relevant form elements:

<input id="author" onkeyup="ReloadNameDiv();" />
<textarea id="text" rows="10" cols="50" onkeyup="ReloadTextDiv();">
</textarea>

This just left 2 things. First, as the script relies on the ‘onkeyup’ event, it won’t preview the author name when the ‘remember personal info’ cookie and javascript autofills the form. To get around this, I reused a piece of the Movable Type code to make sure that the name is autofilled in the preview as well:


<script type="text/javascript">var authname = getCookie("mtcmtauth"); 
document.write(authname)
</script>

Finally, it needed the latest date to be added. You can do this with Javascript too, but I’ve used a simple php code. If the format I’ve used doesn’t fit with yours, all you need to do is change the ‘d.m.y’ part. There is a full list of all the formats at php.net.

Posted on <?php $today = date("d.m.y");
print "$today"; ?>

Here’s there whole preview markup:


<div class="comment">
<h3>preview</h3> 
<p class="author"><a href="#" id="NameDisplay">
<script type="text/javascript">var authname = getCookie("mtcmtauth"); 
document.write(authname)
</script></a>:</p>
<div id="TextDisplay"></div>
<p class="posted">Posted on <?php $today = date("d.m.y");
print "$today"; ?></p>
</div>

Its not perfect, but it negates the need to use a basic cgi page, and retains the sites look and navigation content.

Comments | RSS

No.1

Scrivs said 2233 days ago:

Interesting idea. However, in Firebird 0.61 when I press the tab key it jumps me back up to the top of the page instead of the next form field. Not sure if it is the site or the browser. I am guessing that this is simply best used for when someone wishes to use html in a comment or something to see if it works because I can't understand why someone would simply type text and try to read it in two different boxes.

Great read nonetheless.
No.2

Dris said 2233 days ago:

Well, those who don't normally use the "preview" button probably won't notice the benefits. I like it. It takes away the obscurity most sites have as to how HTML is treated in comments. Usually I just avoid HTML altogether if there's no preview button. It's nice not to have to load another page just to check the formatting. I'll bet it helps that tiny little bit on bandwidth as well (though marginal it may be).
No.3

Josh Jarmin said 2233 days ago:

Very cool idea. This could be the wave of the future of blogging.
No.4

quis said 2233 days ago:

I'm getting the same tabbing problem in Firebird 0.7 but other than that it works really well, and best of all isn't invasive.
No.5

Mark Wubben said 2232 days ago:

Looks cool. I might just implement this on my site (some updates coming up). It seems WordPress doesn't have a preview function, and this is much better a solution than writing a serverside preview.



If you run the preview through an XML parser, you can make sure valid XHTML is submitted. It would also be nice to validate the input, thus the user is sure his comment is submitted and displayed correctly. But.. this might be overkill. Validating as the user types can be pretty annoying as well.

No.6

Mike said 2232 days ago:

Man, this is unbelievable. I'm going to implement this in my blog ASAP.

Great write up!

Mike
No.7

Matt Burris said 2231 days ago:

Wow, very neat. A great way to proofread your own typing -- sort of. ;)

Good job on the script. It's always fascinating to find new things that enhances our blogs.
No.8

Jon HIcks said 2231 days ago:

The problem with tabbing form fields could be due to my experimentation with 'tabindex' attributes. I've taken these out now and it certainly works OK with Firebird on OS X. Can someone check on PC for me?
No.9

Mark Wubben said 2230 days ago:

Works a treat, Jon.
No.10

Shmuel said 2230 days ago:

"Its not perfect, HTML still doesn’t get previewed" -- What do you mean by this?

Some HTML seems to get previewed just fine other HTML freaks out the preview box entirely. For example an 'i' tag shows up in the preview box fine but a 'blockquote' tag results in the entire comment disappearing from the preview box so that it looks as though nothing has been typed at all. Any ideas on how to improve this? I mean even some sort of error message replacing the comment would be better than it simply disappearing.
No.11

Jon Hicks said 2229 days ago:

Sorry Shmuel, you're right - it does get previewed now (bad copy checking - I was referring to the original version. I've taken that bit out.

As for allowing blockquote tags - can't help there I'm afraid. As I've pointed out, I didn't write this script, I'm just showing folks what I've discovered and where to find it!
No.12

Dris said 2229 days ago:

Well, it's the equivalent of throwing unclosed elements into your page, so many browsers will freak out. You could add a mechanism to the script that detects tags that are typed into the comments and closes them in the preview until the user types in the closing tag manually. That would work perfectly. Unfortunately, I know by experience the tediousness of writing such a script without bugs (but maybe some Javascript guru knows how to do this right).
No.13

Ryan Johnson said 2229 days ago:

Processing on every keyup is pretty intensive on plenty of computers (I'm on a g4 over here). Try onBlur instead of onKeyUp, the illusion of realtime typing ceases with onKeyUp =) Otherwise, quite an excellent idea though!
No.14

Dris said 2228 days ago:

That would actually be a pretty good solution, only displaying the preview after it's done being typed and the user tabs over. Cool idea!
No.15

Jon Hicks said 2228 days ago:

It would also stop the slightly annoying 'flickering'. Thanks for the idea, I'll look into it.
No.16

Patrick Haney said 2224 days ago:

I'm noticing that hitting ENTER to leave an extra blank line between paragraphs doesn't show up in the preview. You'd have to put BREAK tags in, and that's not useful. I would expect a break in the text box would show up in the actual post as a break.

Second paragraph. Also, tabbing works alright for me until I tab past the comment box, which takes me to the top of the page. Firebird 0.7 on Windows XP Pro.
No.17

Jon Hicks said 2224 days ago:

Ah well, you can't please everyone.

The Hickensian is the journal of Hicksdesign, a creative partnership of Jon & Leigh Hicks. Read more about us.

Journal RSS Feed

POWERED by FUSION

Elsewhere

The Rissington Podcast - weekly shenanigans with Jon Oxton

Hicksmade - unique handmade goods by Leigh Hicks

Hickr | RSS

Contact

Hicksdesign

Island House

Lower High Street

Burford

Oxfordshire, UK

OX18 4RR

+44 (0)7917 391 536

Click to download my vCard Download vCard

I am currently working full-time with Opera, and not taking on any new projects