Dynamic Comment Previewing
(This article was cross-posted here at Learning Movable Type.)
I've never liked the idea of a seperate comment preview page. Besides being overkill, it often gets missed during upgrades and becomes just another bug to squash.
So after picking up some good ideas from Mike Industries, I decided to toss it to the curb and show our commenters just exactly what their comment will look like as they type.
I give you, dynamic comment previewing in three easy steps:
Step 1: Modify mt-site.js
Add the following code to the end of your mt-site.js and rebuild it:
// Dynamic Comment Preview - Kudos to Mike Industries for the inspiration!<br />
// D.C.P. - Comment Text<br />
function ReloadTextDiv() {<br />
document.getElementById('TextDisplay').innerHTML = '<p>'+document.getElementById('comment-text').value.replace(/(\r\n|\n)/g,'<br />').replace(/(<br \/>){2,}/gi,'<'+'/p><p>')+'<'+'/p>';<br />
}<br />
// D.C.P. - Comment Author<br />
function ReloadNameDiv() {<br />
document.getElementById('NameDisplay').innerHTML = document.comments_form.comment-author.value;<br />
}<br />
// End Dynamic Comment Preview
This is the javascript that powers the live comment preview. Our comment form will call to these functions while you're typing in the author field or the comment field and update the comment preview in realtime.
Step 2: Modify individual entry archives
We're now going to add the live preview "box" to your site. Provided here is the HTML that will work with the standard Movable Type templates. This should come directly after the closing tag of your comment form.
...</form>
<div class="comments-content" id="preview">
<h3 class="comments-preview">Comment Preview</h3>
<div class="comment">
<div id="TextDisplay"></div>
<p class="comment-footer">Posted by:
<span class="author"><a href="#" id="NameDisplay">
<script language="Javascript" type="text/javascript">
<!--
var authname = getCookie("mtcmtauth");
document.write(authname);
//-->
</script></a>
</a></span>
</p>
</div>
</div>
You may need to modify the structure to fit your own style. Essentially, whatever element has the id "TextDisplay" will be filled with the comment text and whatever element has the id "NameDisplay" gets filled with the author name.
Step 3: Modify IEA's once more
We need to "activate" our form fields, or set them up to call to the D.C.P. javascript we put in mt-site.js earlier. Simply find the comment author field and add a call to the name reload function onkeyup. It should look something like:
<input id="comment-author" name="author" size="30" onkeyup="ReloadNameDiv();" />
Let's do the same thing for our comment text field. This time we're calling to the text reload function.
<textarea id="comment-text" name="text" rows="10" cols="30" onkeyup="ReloadTextDiv();"></textarea>
Just be sure that the id's you use on there form elements match the id's in your mt-site.js or else you wont get any updating.
And finally, the moment we've all been waiting for. Remove the preview button from your template. It looks something like this:
<input type="submit" accesskey="v" name="preview" id="comment-preview" value="Preview" />
Rebuild and give it a try. Gives commenting a whole new 'zing', doesn't it?
InterAction:
23 August 20062. LDY:
Hmmm. How does this look?
Very cute!
24 August 20063. Rob Buckley:
Just noticed the anchor tag in step 2 doesn't have a close tag after it. Don't know whether that will affect the behaviour of the script, but it will be invalid XHTML.
24 August 20064. Rob Buckley:
That should have been . Sorry!
29 August 20065. salguod:
Live preview is really nice, especially if you are adding HTML tags since you can see if tag typos are futzing up your comment.
I've been using a live preview on my site for a while now, about a year and a half. I managed to hack together two things from a WP blog I read, formatting buttons and the live preview. I cobbled it together into something I called EasyComments. I think the preview thing works the same as yours, but to be honest I don't completly understand it.
One thing I found with the live preview is that it's usefulness is hampered by the preview being at the bottom of the screen. As you type, the preview is added off screen as the window expands down. In order to see it, you have to keep scrolling. In many cases, I suspect folks didn't even know it was there since it was 'below the fold' if you will.
When I upraded to MT3.2, I tweaked my CSS, my individual entry archive and the preview to allow the live preview to happen along side the entry box. This required a wider layout, which I wanted anyway, to make space for the two collumns. I think it worked out nicely, no more scrolling to see the preview as you type (unless the comment is really long).
BTW - I also was not able to log in with Typekey.
29 August 20066. salguod:
Hmm, that's odd, it didn't show me as signed in to Typekey when I typed my comment, but it did when I posted it.
29 August 20067. Jesse Gardner:
Rob: Thanks for you comments. I managed to get the TypeKey redirect working. I also stuck that where it belonged, good catch!
Salguod: I agree... sticking it way down low is annoying... perhaps if I put it above the post button; that would help people "discover" it. Putting it on the side wouldn't work for my standard layouts, but I'm sure if I had timeI could dream up a good way to preview it... Anyhow, I'm still trying to iron out this TypeKey authentication issue. I fear it has something to do with my JavaScript...
30 August 20068. salguod:
Yeah, you've gotta be willing to redo your layout to do what I've done. I took advantage of a resdesign of my site's layout when moving to MT3.2 to implement that.
31 August 20069. Stephanie:
Good idea. :)
1 September 200610. Rob Buckley:
1) Still can't log into the page with TypeKey. I click "sign in" and just get returned to the page without the fields being filled or removed
2) If I type my name as "Rob Buckley", only the Rob appears
3) Looking at the code, if you replace this:
var authname = getCookie("mtcmtauth");
document.write(authname);
with
document.write(commenter_name)
it should pick up the TypeKey name as well as the cookie info if it's available.
13 September 200611. Constance:
I haven't tried this one yet. Hmmm... can I also do this too in other platforms, say, Wordpress?
13 September 200612. Jesse Gardner:
Absolutely. This really isn't platform dependant. It just needs javascript to work; whether your running WP, EE or your own custom thing is no matter.
7 October 200613. Fabri:
Cool !!! very interesting
2 November 200614. Max Watson:
I'm having the same issue with wondering if I'm logged in with typekey here on your site.
Anyway, I'm not getting the "name" field filled in with the installation at my site too. Guess I'll just yank out that part of the script.
30 January 200715. johntunger:
I'm really just commenting to view the code in action here.
I've got the cocomment extension enabled in firefox and it overlaps the comment enter field in a disconcerting way... for a moment I didn't realize I could type here because the extension was displaying with (and covering over) this field.
21 May 200716. smartpill:
For those having problems getting the "author's name" to show, looks like this line in mt-site.jsdocument.comments_form.comment-author.value; needs to be changed to document.comments_form.author.value;
YourThoughts?
(Minutia)
- Author:Jesse
- Published:Aug 23, 2006
- Chapters:
- Previous:
- Next:
GetUpdated
ElseWhere
AllChapters
- 2 articles in the chapter Accessibility
- 6 articles in the chapter Announcements
- 2 articles in the chapter Apple
- 5 articles in the chapter Blogging
- 7 articles in the chapter CMS
- 1 articles in the chapter Code
- 3 articles in the chapter CSS
- 16 articles in the chapter Design
- 1 articles in the chapter DIY
- 4 articles in the chapter Downloads
- 1 articles in the chapter Freebies
- 2 articles in the chapter Gadgets
- 5 articles in the chapter Journeys
- 1 articles in the chapter Miscellany
- 1 articles in the chapter Mobile
- 59 articles in the chapter Movable Type
- 2 articles in the chapter Plugins
- 1 articles in the chapter Print
- 3 articles in the chapter Projects
- 1 articles in the chapter Reviews
- 2 articles in the chapter SEO
- 8 articles in the chapter Social Networking
- 1 articles in the chapter Standards
- 4 articles in the chapter Writing
BiteSize.blog
ReaderFavorites
- CSS Image Framing
- Tools of the Web Design Trade, Pt.1: Where to Begin
- Tools of the Web Design Trade, Pt.2: Building Trust
- 10 Tips For Creating Website Mockups In Photoshop
- Movable Type As A Desktop App
- Redesign, Part 1: The Logo
- Redesign, Part 2: Stylesheet Philosophy
- The Social Graph in Plain Language
- Writing Your Own Autobiography: The New Persistence of Information
- Designing eComm 2008


















23 August 20061. Rob Buckley:
Thanks. I've tried it, but it wouldn't grab my name from my TypeKey authentication. Incidentally, I couldn't log in to get TypeKey authenticated here, either! The redirect sent me to a 404 page at plasticmind.com after I logged in.