htmlspecialchars() in Javascript

Discussion in 'JavaScript' started by winterheat, Sep 4, 2008.

  1. #1
    it seems that we need to write our own version of PHP's htmlspecialchars() if we want to do it in javascript huh?

    let's say if we want to do things like

    document.getElementById("divCode") = "if (a < 10) { ... }";

    or

    document.getElementById("divCode") = "use <span> if you want to ... Use &copy; if you want to...";

    then we will need to change the "<" into &lt; (and so forth for the "&" etc) so it will be handy if there is a built-in function that will do that. Looks like prototype.js doesn't have that if i do a search on google using

    htmlspecialchars site: prototypejs.org

    Thanks very much!
     
    winterheat, Sep 4, 2008 IP
  2. MMJ

    MMJ Guest

    Messages:
    460
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #2
    document.getElementById("divCode").appendChild(document.createTextNode("if (a < 10) { ... }"));
    Code (markup):
     
    MMJ, Sep 5, 2008 IP
  3. temp2

    temp2 Well-Known Member

    Messages:
    1,231
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    150
    Digital Goods:
    2
    #3
    temp2, Sep 5, 2008 IP