最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How prettify.js encode HTML to show on page - Stack Overflow

programmeradmin3浏览0评论

I am using Prettify.js and css for my web site but it is not working for HTML

Let's say I have HTML like this:

<div style="padding:3px 0px">
  <asp:Label ID="lblTotalAns" runat="server" Text="0 Answers" CssClass="ansHeading" />
</div>

How it will render by prettify?

I am using Prettify.js and css for my web site but it is not working for HTML

Let's say I have HTML like this:

<div style="padding:3px 0px">
  <asp:Label ID="lblTotalAns" runat="server" Text="0 Answers" CssClass="ansHeading" />
</div>

How it will render by prettify?

Share Improve this question edited Mar 8, 2012 at 14:51 tonymarschall 3,8823 gold badges33 silver badges52 bronze badges asked Feb 8, 2012 at 17:13 Ali AdraviAli Adravi 22.8k9 gold badges91 silver badges94 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 15 +25

You have to change < to &lt; and > to &gt; otherwise your code gets rendered by the browser. So your code has to look like this:

<pre class="prettyprint">
  &lt;div style="padding:3px 0px"&gt;
    &lt;asp:Label ID="lblTotalAns" runat="server" Text="0 Answers" CssClass="ansHeading" /&gt;
  &lt;/div&gt;
</pre>

I have created a simple example with your html source: http://jsfiddle/aSb76/

This can also be seen in prettify tests source and the result page.

The basic setup on howto include prettify.js is described in the README.

I have no clue about escaping in ASP, but the first search result in Google for something like 'asp encode html entities' brought up <%= Server.HTMLEncode(sValue) %>. Maybe you can use this.

I would like to propose an alternative solution through which you could achieve the same result (that I assume) you would like to get (= syntax highlighted HTML code shown embedded in a webpage).

Github has a neat service (called Gist) that is used for saving little code snippets online and it allows:

  1. syntax highlighting
  2. embedding of code snippet in your HTML (with 1.)

Here you can see your example markup on Github Gist: https://gist.github./2012701

Here you can see it embedded in HTML: http://jsfiddle/6YHDu/

If the code you want to show syntax highlighted is generated dynamically (not entered manually), then forget my proposal and go with Prettify.js, otherwise Gist is a very user friendly alternative.

You would use it like this:

In your <head> element:

<link href="prettify.css" type="text/css" rel="stylesheet" />
<script type="text/javascript" src="prettify.js"></script>

In your <body> element:

    <body onload="prettyPrint()">
      <div style="padding:3px 0px">
       <asp:Label ID="lblTotalAns" runat="server" Text="0 Answers" CssClass="ansHeading" />
      </div>
     </body>

I agree with @tonymarschall I found this alternative if you are looking for one. They say you can use instead of and you wouldn't have to decorate your code with ugly &lts Maybe there's some escape involved with Prettify.js as well? After all it is made by Google!

发布评论

评论列表(0)

  1. 暂无评论