Live comment previewing using the jQuery library
OK, I'll admit it, I'm like 2 years behind the bandwagon, but I'm totally addicted to jQuery right now. I can hack my away around Perl and PHP well enough, but I've never really done much with javascript other than tinkering with form elements here and there. I just recently, about two weeks ago, bought the book jQuery in Action from Amazon and I can't put it down.
jQuery makes everything so ridiculously easy, it keeps it clean and "unobtrusive", i.e. keeps the "behavior" out of the mark-up. So anyways, I realized that sNews needed a live comment preview, so I put this together, I've only tested this with jQuery 1.3.1, so if you don't already use jQuery, either download the library and serve it locally from your server, or use Google's Ajax libs... go ahead, it's encouraged.
Assuming you want to use Google, insert this code into the head of your index.php;
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.min.js"></script>
Then simply place this code below it, still within the head of your index.php file;
<script type="text/javascript">
$(document).ready(function() {
$('#text').one('focus',function() {
$('#text').parent().after('<div><label for="comment-preview">Comment Preview</label>:</div><div id="comment-preview"></div>');
});
var $comment = '';
$('#text').keyup(function() {
$comment = $(this).val();
$comment = $comment.replace(/<\/?[^>]+>/g,"").replace(/\n\n+/gi, '<br /><br />'); // this strips tags and then replaces new lines with breaks
$('#comment-preview').html( $comment );
});
});
</script>
Add the styles below to your style sheet if you like, that's it, now you have a basic comment preview for your sNews, once I get some more jQuery skills under my belt, I'll try and work the gravatars into the preview.
#comment-preview {
background:#fff;
border:1px solid #ccc;
padding:10px 5px;
width:97%
}
Comments
RSS Comments Feed
Patric
Oh, I see now below this text field. But I don't get that. Mhm... wonder what could be the dilemma here. Have to go check it out.
Hehehe
Matt
Patric
Now we both know that IE sucks, but still. Peep's use it and a security warning from browser isn't allways a good thing when it comes to ordinary people.
So, twice things ;) Is it possible to validate (like validation) and Is it possible to get rid of that warning in IE ?
Damn, that bugger asks alot ;)
Matt
The code is valid, that W3 online validator just doesn't understand that we aren't putting the preview HTML in the head.
That's an easy fix, just put the preview stuff in a separate .js file like mine here;
<a href="http://mdj.us/js/addin/cpreview.js">http://mdj.us/js/addin/cpreview.js</a>
then call it like this;
<script type="text/javascript" src="js/cpreview.js"><script>
I can't replicate any IE ActiveX warning from any of my machines, that would likely be a high security setting within IE and would probably trigger on any site, it wouldn't be specific to this little nugget of jquery.
I will investigate a little further when i get some free time, do you get the activeX warning on my site?
Patric
Regarding the Active X thingy, I think it must be the FF addon. And possibly security settings on work comp.
I do not see naything on your site.
Matt
Regarding the Firefox IE tab add-on, I use similar apps myself, "IE Tester" and "Multiple IE" to view my sites in various versions of IE, but it turns out that neither of those emulators renders quite the same as the real version of IE would, so I don't really trust them 100%.
slemborg
Matt
slemborg
FredK
Matt
Something along these lines; http://jsbin.com/ofaqu
FredK
Matt
I do that all the time myself.
We call it a PEBKAC... "Problem Exists Between Keyboard And Chair"
:)
Sven/Philippe
legal service