0

I just want to show a basic html document in my website, and the text includes script tags. Is there a way to show:

<script>document.write("hi");</script>

Instead of hi?

Basically i want to NOT execute the javascript input.

j08691
  • 204,283
  • 31
  • 260
  • 272
  • 1
    You could encode those entities though. – AloHA_ChiCken May 06 '21 at 07:18
  • 1
    You have to treat it as a string and escape it: https://stackoverflow.com/questions/6234773/can-i-escape-html-special-chars-in-javascript – Sergiu Paraschiv May 06 '21 at 07:19
  • 1
    [Duplicate](//google.com/search?q=site%3Astackoverflow.com+display+html+tags+in+html) of [Display HTML snippets in HTML](/q/2820453/4642212). Entities are discussed in any [introduction to HTML](//developer.mozilla.org/en-US/docs/Learn/HTML/Introduction_to_HTML/Getting_started#entity_references_including_special_characters_in_html). – Sebastian Simon May 06 '21 at 07:22
  • @Codenewbie Anything different? – AloHA_ChiCken May 06 '21 at 07:25
  • @AloHA_ChiCken yep , removed deprecated and edited – KcH May 06 '21 at 07:28

2 Answers2

2

You could use a pre tag but need to

replace all < signs with &lt; and all > signs with &gt;

<pre>
    &lt;script&gt;document.write("hi");&lt;/script&gt;
</pre>
KcH
  • 3,302
  • 3
  • 21
  • 46
0

Use the &lt; html entity to escape the tag opening interpretation of the opening angle bracket.

&lt;script>document.write("hi");&lt;/script>
loop
  • 825
  • 6
  • 15
  • Do your own answer instead of totally overwriting somebody else's answer. – loop May 06 '21 at 17:26
  • on another side, your answer is currently in the "low quality" queue. The edited was not. You should accept the edit, imho. – manuell May 07 '21 at 08:02
  • On the other hand one should create his own answer instead of totally overwriting somebody else's answer, if one's answer is totally unrelated to the one he is overwriting, even if it's of 'low quality'. – loop May 07 '21 at 10:03
  • And, unrelated to that, my answer is currently the only one that is working and it contains the minimal solution to the problem at hand. The other solution contains confusing
     tags which make the output different from what was wished for.
    – loop May 07 '21 at 10:06
  • I welcome any independent answer that puts more effort (than I did) to answering this question. – loop May 07 '21 at 10:09
  • Ok, now +1 (short solution is cool, with explanations of why it works, it's better) – manuell May 07 '21 at 13:56