Skip to content Skip to sidebar Skip to footer

HTML Tag That Causes Other Tags To Be Rendered As Plain Text

I'd like to add an area to a page where all of the dynamic content is rendered as plain text instead of markup. For example: Hello Wor

Solution 1:

You can do this with the script element (bolded by me):

The script element allows authors to include dynamic script and data blocks in their documents.

Example:

<script type="text/plain">
This content has the media type plain/text, so characters reserved in HTML have no special meaning here: <div> ← this will be displayed.
</script>

(Note that the allowed content of the script element is restricted, e.g. you can’t have </script> as text content (it would close the script element).)

Typically, script elements have display:none by default in browser’s CSS, so you’d need to overwrite that in your CSS, e.g.:

script[type="text/plain"] {display:block;}

Solution 2:

The tag used to be <XMP> but in HTML 4 it was already deprecated. Browser's don't seem to have dropped its support but I would not recommend it for anything beyond quick debugging. The MDN article about <XMP> lists two other tags, <plaintext> and <listing>, that were deprecated even earlier. I'm not aware of any current alternative.

Whatever, the code to encode plain text into HTML is pretty straightforward in most programming languages.

Note: the term similar means exactly that—all three are designed to inject plain text into HTML. I'm not implying that they are synonyms or that they behave identically—they don't.


Solution 3:

You can use a function to escape the < >, eg:

'span.name': function(){
return this.name.replace(/</g, '&lt;').replace(/>/g, '&gt;');
} 

Also take a look at <plaintext></plaintext>. I haven't used it myself but it is known to render everything that follows as plain text(by everything i mean to say it ignores the closing tag, so all the following code is rendered as text)


Solution 4:

No, that's not possible, you need to HtmlEncode it.

If your using a server-side language, that's not really difficult though.

In .NET you would do something like this:

string encodedtext = HttpContext.Current.Server.HtmlEncode(plaintext);

Solution 5:

In my application, I need to prevent HTML from rendering

"if (a<b || c>100) ..."

and

"cout << ...".

Also the entire C++ code region HTML must pass through the GCC compiler with the desired effect. I've hit on two schemes:

First:

//<xmp>
#include <string>
//</xmp>}

For reasons that escape me, the <xmp> tag is deprecated. I find (2016-01-09) that Chrome and FF, at least, render the tag the way I want. While researching my problem, I saw a remark that <xmp> is required in HTML 5.

Second, in <head> ... </head>, insert:

<style type="text/css">
    textarea { border: none; }
</style>

Then in <body> ... </body>, write:

//<br />  <textarea rows="4" disabled cols="80">
#include <stdlib.h>
#include <iostream>
#include <string>
//</textarea>  <br />

Note: Set "cols="80" to prevent following text from appearing on the right. Set "rows=..." to one more line than you enclose in the tag. This prevents scroll bars. This second technique has several disadvantages:

  • The "disabled" attribute shades the region
  • Incomprehensible, complex comments in the code sent to the compiler
  • Harder to understand
  • More typing

However, this methhod is neither obsolete nor deprecated. The gods of HTML will make their faces to shine unto you.


Post a Comment for "HTML Tag That Causes Other Tags To Be Rendered As Plain Text"