Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript Is there an alternative to replace < > with lt; and gt;

  • This topic is empty.
Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #39374
    JohnMotylJr
    Participant

    Im using prettyprint aka prettify to display code. Rather than having to convert all of my < and > entities i am using this function:

    String.prototype.escapeHTML = function() {
    return(
    this.replace(/ replace(/>/g,'>').
    replace(/"/g,'"')
    );
    };
    var conv = document.getElementById('conversion');
    if (conv) {
    convl.innerHTML = conv.innerHTML.escapeHTML();
    }

    This works quite well for preventing the html tags from rendering but when i use with prettify, it displays as a massive block of code without styles. Im sure there is something conflicting, but rather, are there any alternatives that are more popular for the fix im trying to attempt?

    jsfiddle: http://jsfiddle.net/john_motyl/2QFnB/ (swap the id to see what im talking about)

    #107914
    Mottie
    Member

    I’m not seeing the issue you are describing, but I would think if you escaped the HTML before running prettyPrint your problems should go away.

    Just to make sure you know that all syntax hilighting scripts add something like a span around bits of the code to colorize it, so it sounds like those elements are being converted by the escapeHTML script above and messing up the look.

    #107941
    JohnMotylJr
    Participant

    @mottie, your absolutely correct. I was calling prettyprint() before trying to escape the html entities. Thanks for pointing that out, i totally overlooked that. Now i call that function inside my if statement and it works perfect. Thanks

Viewing 3 posts - 1 through 3 (of 3 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.