Live Comment Previews
TypePad, Website July 20th, 2003 |The ‘Live Comment Preview’ hack that I use on The Long Letter has now been implemented here on Eclecticism. If you know what I’m talking about, then we’re good to go. If you don’t know what I’m talking about, then click on the “Comments” link to any post, type something in the comment box, and look just below the comment box. It’s a nifty trick.
I originally picked this up from ScriptyGoddess, with help from Phillip. Geeky tech details on my implementation here follow.
All this is is a nice little JavaScript addition to the page. I don’t believe that this will constitute a security risk, but I’m no expert, so use at your own risk.
Obviously, if you do want to use this on your site, you’ll need to have access to your templates.
In the header of the individual entry template, just after the already included JavaScript bits, I added the following code:
<script type="text/javascript">
var newline = /\n/g;
function ReloadTextDiv() { var NewText = document.getElementById("text").value; NewText = NewText.replace(newline, "<br/>"); var DivElement = document.getElementById("TextDisplay"); DivElement.innerHTML = NewText; } </script>
Then, in the body of the template, just after the closing </div> tag following the preview and submit buttons, but before the </form> tag, I added the following:
<br />
<h2>Live Comment Preview:</h2>
<p><span id="TextDisplay">Note: if you're comfortable with HTML, feel free to use it in your comments. If not, just type away. Single returns will be automatically converted into linebreaks (<br/>), double returns will be converted into paragraph breaks (<p>). This text will disappear as soon as you start typing.</span></p>
Lastly, in the textarea tag that defines where the comment text is entered in by a visitor, I added a onkeyup="ReloadTextDiv();" declaration. The full textarea tag should look as follows:
<textarea tabindex="4" id="text" name="text" rows="10" style="width: 80%;" onkeyup="ReloadTextDiv();">
What all this does is actually simple enough. As a visitor enters their comment into the comment box, each time they release a key the onkeyup function calls the ReloadTextDiv JavaScript snippet that I added. This function loads any text inside the comment box (identified by its ID of "text"), replaces any carriage returns with <br /> tags so that line breaks appear correctly, then writes the output into the element identified as "TextDisplay" — in this case, between the span tags I added after the submit and preview buttons.
If you find this useful, feel free to use it in your own pages. While I wouldn’t refuse credit, it really does belong to ScriptyGoddess and Phillip. Enjoy!
[See also: No more 404’s | JavaScript toggle code | MT: Easy comment and entry editing | Cookies finally fixed! | Transitioned ]
« Pirates of the Caribbean: The Curse of the Black Pearl | TPS Syndrome »






July 20th, 2003 at 11:48 pm
This is just a quick test comment to see if rebuilding will break the page.
July 21st, 2003 at 11:42 pm
Live Comment Preview
Thanks to Michael Hanscom over at Eclecticism for this wonderful idea of being able to preview what you type in the comments, all live and on the fly. If you have a Typepad blog, take a look: Live Comment Preview
July 21st, 2003 at 11:45 pm
I love this, it’s so wonderful. It gives the whole commenting system a different feel, and I think overall, it makes it more interesting to comment for readers. Good going!
July 31st, 2003 at 12:31 pm
Live Comment Preview
Thanks to Michael (thanks for all the help) I was able to add Live Comment Previewing! It’s a pretty cool feature that lets vistors watch their comment appear on-site as they type. Leave a comment to test it out.
February 12th, 2004 at 11:15 am
Live Comments
Thanks to Michael, I have implemented Live Comments. They allow you to see your comments as you type them. Please note that although you may see your HTML in the live comments, it will be stripped. Best to preview your…
February 12th, 2004 at 11:33 am
Live Comments
Thanks to Michael, I have implemented Live Comments. They allow you to see your comments as you type them. Please note that although you may see your HTML in the live comments, it will be stripped. Best to preview your…
February 12th, 2004 at 11:34 am
Live Comments
Thanks to Michael, I have implemented Live Comments. They allow you to see your comments as you type them. Please note that although you may see your HTML in the live comments, it will be stripped. Best to preview your…
December 26th, 2004 at 7:59 am
Tweaks
So far, this has been a very productive Christmas Day. I’ve been tweaking the site, fixing small errors/problems mainly, here’s what i did: In the main page, i’ve added a link to the Category where the post was made in,…
June 3rd, 2007 at 6:53 am
test
June 13th, 2007 at 8:28 am
just a test
December 3rd, 2007 at 1:26 pm
just testing this thing
January 8th, 2008 at 2:20 am
hey
February 21st, 2008 at 3:45 pm
test tester
April 30th, 2008 at 5:11 am
hello
May 4th, 2008 at 4:45 pm
zetia ¶
May 6th, 2008 at 2:04 pm
What is the deal? Are people test commenting on an ancient blog?
May 22nd, 2008 at 7:22 pm
sure why not