+ Reply to Thread
Results 1 to 3 of 3

Thread: Weird JavaScript XML Problem

  1. #1
    Twinkie is offline Banned Twinkie is an unknown quantity at this point
    Join Date
    Sep 2007
    Location
    Ft. Lauderdale, Florida
    Posts
    1,389

    Weird JavaScript XML Problem

    I guess I have gotten a lot better at solving my own problems; I have not asked for help here in a long time, but this issue I can't seem to understand although I solved it. I have been a making a support chat script, which works to get data to the client by Database -> PHP -> XML -> JavaScript -> HTML and vise versa. To protect my users, and the script, I used htmlspecialchars() on the inputted text (PDO protects the database). The XML escapes fine but when I load the data from the XML return into the JavaScript, it is unencoded again. I can't find why, there seems to be no documentation on it. I solved the issue by double escaping with htmlspecialchars() to compensate, but I would like to know why.

    The returned XML for the server.
    Code:
    <chat>
      <connection>
        <status>connected</status>
      </connection>
      <updates>
        <message>
          <user>Test1</user>
          <time>1282436270</time>
          <type>user</type>
          <text>&lt;hello&gt;&lt;/hello&gt;</text>
        </message>
      </updates>
    </chat>
    The JavaScript parser.
    Code:
    var messages = chat.lastChild.getElementsByTagName('message');
    for (var i = 0; i < messages.length; i++) {
      var message = new Object();
      for (var b = 0; b < messages[i].childNodes.length; b++) {
         var prop = messages[i].childNodes[b];
         if (prop.hasChildNodes()) {
           message[prop.nodeName] = prop.firstChild.nodeValue;
         } else {
           message[prop.nodeName] = '';
         }
      }
      this.chatbox.createMessage(message['type'], message['text'], message['time'], message['user']);
    }
    The end result in the browser looks blank, but with a closer inspection of firebug you can see that the browser has hidden it as a html tag. Inspecting with firebug, I am sure that the problem does not extend beyond that.
    Last edited by Twinkie; 08-21-2010 at 07:42 PM.

  2. #2
    lemon-tree's Avatar
    lemon-tree is offline x10 Minion lemon-tree has a spectacular aura about
    Join Date
    Nov 2007
    Posts
    1,420

    Re: Weird JavaScript XML Problem

    That makes sense, think of it like this:
    The first encode prevents it from thinking it is XML and makes it parse it as HTML
    The second code prevents it from thinking it is HTML and parses it as plaintext

    However, there is a nice alternative you can use that'll mean you only need to do it once:
    Code:
    <chat>
      <connection>
        <status>connected</status>
      </connection>
      <updates>
        <message>
          <user>Test1</user>
          <time>1282436270</time>
          <type>user</type>
          <text><![CDATA[&lt;hello&gt;&lt;/hello&gt;]]></text>
        </message>
      </updates>
    </chat>

  3. #3
    Twinkie is offline Banned Twinkie is an unknown quantity at this point
    Join Date
    Sep 2007
    Location
    Ft. Lauderdale, Florida
    Posts
    1,389

    Re: Weird JavaScript XML Problem

    Thanks for that, I will look into it

+ Reply to Thread

Similar Threads

  1. Weird Problem
    By devongovett in forum Free Hosting
    Replies: 2
    Last Post: 12-17-2008, 04:28 AM
  2. javascript and external javascript files problem
    By delon in forum Programming Help
    Replies: 6
    Last Post: 04-27-2008, 12:41 AM
  3. Weird Problem...
    By toasterthegamer in forum Free Hosting
    Replies: 2
    Last Post: 10-01-2007, 08:57 PM
  4. Weird ip problem..
    By Friskypants in forum Free Hosting
    Replies: 4
    Last Post: 06-26-2007, 02:38 PM
  5. Weird Problem
    By Spal7e in forum Free Hosting
    Replies: 50
    Last Post: 04-10-2007, 08:10 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers