0

On Stack overflow you can use ctrl k to make HTML look like HTML and not the output of it.

So something like

<div>
    <p>Hello world</p>
</div>

Instead of Hello World

I inspected the web page and it comes out like this

<pre>
  <code>
    &lt;div&gt;
    &lt;p&gt;Hello world&lt;/p&gt;
    &lt;/div&gt;
  </code>
</pre>

I am working on a project where I want to do something similar. I'm assuming it uses some some javascript function to perform this but I'm not really sure, anyone know how this works?

  • 1
    The cause of this is going to depend on the technology that was used. Please provide a link to the problem, or at least more context. How was the HTML added to the page? Did you type it in or was it rendered by some server-side technology? – BishopZ Jul 28 '14 at 22:32
  • `
    ` is for pre-formatted text. If you want to display markup in the `
    ` you need to convert the markup to entities as per your example. You can either do it manually or through scripting JS or php. `html_entites(str)` in php
    – Chris Rymer Jul 28 '14 at 22:32
  • This link is also relevant http://stackoverflow.com/questions/2820453/display-html-code-in-html – Tasos K. Jul 28 '14 at 22:33
  • Use HTML Entities in `
    ` tag/s:   http://www.freeformatter.com/html-entities.html  Note: You should add a 0 before anything like `#39;`. So it's `#039;` which would be PHP `htmlentities()` compatible. The Entity Name should be used whenever possible, as in `<`, `>`, and `"`, for compatibility reasons.
    – StackSlave Jul 28 '14 at 22:33
  • You could do this in PHP by adding header("Content-type: text/plain"), which will force the browser to render the rest of the page content as plaintext. – JTravakh Jul 28 '14 at 22:37

6 Answers6

0

If you have support for PHP you could use the function htmlentities().

Emil
  • 1,786
  • 1
  • 17
  • 22
0

In JavaScript, you can use string.replace() to achieve the same.

For example:

var string = '<h1> Hello </h1>'
console.log(string.replace(/</g, '&lt;').replace(/>/g, '&gt;'));
// "&lt;h1&gt; Hello &lt;/h1&gt;"
Sarbbottam
  • 5,410
  • 4
  • 30
  • 41
0

You are correct that HTML has to be Html-encoded in order to see the brackets. The < symbol is encoded as &lt; (less than), while the > symbol is encoded as &gt; (greater than).

So the question is how do you do that automatically without having to remember to do it.

You may want to check out syntax highlighter:

Syntax Highlighter: http://alexgorbatchev.com/SyntaxHighlighter/

You can also use the string replace and write your own function as sarbbottam suggests.

Aaron
  • 435
  • 3
  • 5
0

You could try this:

function preIt(code){
    code = code.replace(/[&<>]/g, function(tag){
        return {'<' : '&lt;', '>' : '&gt;', '&' : '&amp;'}[tag];
    });
    return "<pre>"+code+"</pre>";
}

Usage:

document.write(preIt("<div><p>Hello world!</p></div>"));

The function takes HTML code as its input and then performs a RegEx replace on the code. It uses a callback function to replace the matched characters with the appropriate HTML entity code.

Alternatively, you could implement the code in this answer.

Community
  • 1
  • 1
noahnu
  • 3,479
  • 2
  • 18
  • 40
0

StackOverflow is merely moving the selected lines over using 4 spaces when you select lines within the <textarea> element, which then is used as markup that can be detected via JavaScript. It would look something like the following:

<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<textarea id="myTextarea" style="width:500px;height:400px;"></textarea>
<button id="clickme">Click me!</button>

<script>
    $('#clickme').on('click', function() {
        var value = document.getElementById("myTextarea").value.replace(/(\n|^)[ ]{4}([^\n]*)/g, function(c) {
            // console.log('found! c ==', c, '; arguments ==', arguments);

            var lineData = arguments[2];

            return "<pre><code>" + lineData.replace(/</g, '&lt;').replace(/>/g, '&gt;') + "</code></pre>";
        });

        // console.log('value ==', value);

        $('body').append(value);
    });
</script>

In the <textarea> place text such as the following, making sure that 4 spaces lead before each line that needs to be formatted as displayable HTML:

<div>
    Hello world!<br />
</div>

Then finally click the "Click me!" button. jQuery was used in this code example for brevity.

Steven
  • 3,526
  • 3
  • 18
  • 28
0

So I ended up using the .html() method of jquery to format the html

$( "p" ).click(function() {
  var htmlString = $( this ).html();
  $( this ).text( htmlString );
});

here's the documentation http://api.jquery.com/html/

and a fiddle http://jsfiddle.net/arh6E/