Tuesday, June 5, 2007

Tech Q&A: How do I enter less/greater than in blog comments?

I've seen this question various places around the blogosphere, so I thought I would address it as the first in a new "Tech Q&A" series. If you've ever tried to post the "<" or ">" characters in blog comments, you've undoubtedly found that these symbols (and anything between them) mysteriously disappeared. Why is that?

Well, when you type in your comments you are entering what is referred to as "plain text." That means that, while it's still in the comments box, your browser views whatever you type in as being literal Roman characters or symbols. Something for humans to read, in other words. When you type left or right carats here, there's no problem.

The problem arises when that plain text is submitted to blogger. Blogger no longer treats it as plain text, but rather starts treating it as HTML markup. This is actually very helpful, because it let's you embed links and images in your comments, and/or allows you to italicize or bold words using those tags.

The only problem with this approach is that if the commenter has entered certain characters -- most notably the left or right carat -- then content after the < will disappear. If there is a > that follows it, then content will become visible after that. I called this a problem, but really this is just how the Internet works in such situations; I don't think that there is much blogger could do differently save possibly not allow HTML in comments at all (which would be bad), or load their rich text editor on the comments page (which would be bulky, and thus undesirable). So I think that they're doing about the best they can reasonably do at this point.

What if you really need to display that < or >, though? For instance, what if you're writing out a math equation or trying to show someone else how to create some HTML code? Fear not, the browser programmers of yore thought of this long ago. There is a special character sequence that tells the browser to show a left or right carat to the user, but that carat will be ignored by the browser. My recent link to the ASCII Codes reference includes not only these sequences, but many more.

Here are the relevant codes:

When you want to show a literal "<" in the posted comments, type &lt; instead.
When you want to show a literal ">" in the posted comments, type &gt; instead.

Those character codes probably seem incomprehensible, so let's break them down. First, there is an opening character that tells the browser that a character code is coming; that's the ampersand. Then comes the actual character code itself, which is specific to the character you want to print, and then comes the closing character that tells the browser that your character code is done; that's the semicolon.

In the specific case of the above examples, the "lt" stands for "less than," and the "gt" stands for "greater than." Easy to remember, right? Feel free to ask for further clarification in my blog comments here, or even to try our the character codes there for yourself if you want to. Also, if anyone has topics that they'd like to see in a future Tech Q&A feature, please send them in.


Anonymous said...

Lets see

As i thought, it tells me it cannot go through, the tag is broken

Christopher M. Park said...

Here's an example of it working: <