Wed 14th Sep 2005
Comment Preview for Blogsome!
Posted in the evening, filed under Blogsome , JavaScript.I’ve implemented Comment Previewing from Blogsome blogs. There is no way to install 3rd party plugins in WordPress MultiUser, so everything has to be done in JavaScript.
But really, the plugin is just JavaScript packaged up nice anyway.
You need four code fragments, all in your comments.html file. The first one I suggest goes after the line:
{if 'open' == $post->comment_status}
Because it is a JavaScript that uses { and }, you need to remember the {literal} tags.
{literal}<script type="text/javascript">
<!--
function ReloadTextDiv()
{
var UseInstantPreview = document.getElementById("use_instant_preview").checked;
if( UseInstantPreview == true )
{
var NewText = document.getElementById("comment").value;
splitText = NewText.split(/\n/).join("<br />");
var DivElement = document.getElementById("commentpreview");
DivElement.innerHTML = splitText;
}
}
// -->
</script>{/literal}
It probably doesn’t matter exactly where it goes, but having it after the {if} clause means it will only be included in the file if comments are open.
The second code fragment is:
onkeyup="ReloadTextDiv();"
And it goes in the <textarea id=comment … > tag. In context, mine is:
<textarea name="comment" id="comment" cols="60" rows="12" tabindex="4" onkeyup="ReloadTextDiv();"></textarea>
The third fragment is the checkbox for enabling or disabling Comment Preview. It can go wherever looks good.
<input onclick='ReloadTextDiv();' name="use_instant_preview" type="checkbox" id="use_instant_preview" value="1" checked /> <label for='use_instant_preview'>Use Instant Preview</label>
The final fragment tells the browser where the Comment Preview goes:
<div><p id="commentpreview"></p></div>
And it goes where you want the Comment Preview to actually go. I suggest just after the Submit button, or other instructions.
Righto, all of that in there, and it should work. You may need to add an entry to your Site Style Sheet to make the Comment Preview box look the same as your actual comments box.
•
If you wanted to, you can also have a Preview button that will preview what is in the text area then, rather than having Live Preview. This is good for a couple of reasons: I find that long comments slow down when trying to update every keypress.
The big problem is that you cannot have a normal input field inside the form set to do this, it will just try to submit it (or at least, mine did!). So, I’ve replaced my input tags in my form with the following (You may need to insert name='commentform' into the form start tag.):
<a class='button' href='javascript:document.commentform.submit();'> Post Reply </a>
<a class='button' href='javascript:ForceReloadTextDiv();'> Preview </a>
You’ll also need to add the following in just after the ReloadTextDiv function definition:
function ForceReloadTextDiv()
{
var NewText = document.getElementById("comment").value;
splitText = NewText.split(/\n/).join("
“);
var DivElement = document.getElementById(”commentpreview”);
DivElement.innerHTML = splitText;
}
Ugh. The new
class='button'links really do look horrible in Internet Explorer 5 (PC). I already knew that it does not seem to respect theborder:CSS tag (Edit Post links, RSS feed links) on <span> HTML tagsEven the
<textarea>is not the right size: not sure if that’s a resolution issue, or just the fact IE is crap. The Scroll bars are missing, and it needs to be narrower. I think it’s because I changed the width to 100% from 80% so it looks nicer in Camino.Actually, the About Box even doesn’t look to be the right size.
At least in IE5 the Preview box does not appear until you click Preview, or have the checkbox enabled.
Note: the
{$smarty.now}thing seems to not be using +0930. I’ll test this on some other machines and see what’s going on. I may need to add(9.5 * 60 * 60)to$smarty.nowto get the right time.12 hours, 2 minutes after the fact.
i have a lot of dudes.
i have used your four fragments in comments.html. but it don ‘t work well.
In quicktags how put an other style? the buttoms are bffffffff
thanks
1 day, 3 hours after the fact.
Hello Matt, i have the same problem as fiys. I can’t have your code to work on my site though i have followed all the instructions. Must have something to do with my own source code i think but can’t figure where.
I will try again till i got it to work (hope so).
Thanks anyway for sharing all those useful bits of code on your own site.
2 days, 11 hours after the fact.
I’ll check that the code posted okay: I may have mangled it when getting it ready for HTML viewing.
1 week, 3 days after the fact.
Okay, there seems to be some extra
\placed in before". Remove them all and it should all work. I’ll fix the post so it looks right too.Sorry.
1 week, 4 days after the fact.