2

I used XMLHttpRequest() to get C source code from server.

return HttpResponse(code, mimetype='text/x-c')

and I got correct source code when I checked it using alert(code).

like this

enter code here
#include<stdio.h>
int main(){
    .....
}

and I split it into each lines in javascript.

var arr = code.split('\n');
window.document.getElementById("id").innerHTML = arr;

the result is like this.

#include
,int main(){
,    ....
,}

I can't find <stdio.h> I don't know why.... I dont think it's because of '<' character. Cuz in for loop, I can find '<' character.

what's the problem?

user2659088
  • 133
  • 1
  • 7

2 Answers2

4

It's the '<' character.

You're setting HTML. Every browser parses stuff between '<' and '>'. In your case, it's finding what it thinks is the DOM element "stdio.h". No browser knows what to do with it, so it ignores it, instead of rendering it.

See Convert special characters to HTML in Javascript for ways to escape the string you receive properly.

Community
  • 1
  • 1
Scott Mermelstein
  • 15,174
  • 4
  • 48
  • 76
  • but i can find '<' characters in for loop or other lines. I dont know why split function doesnt work.. – user2659088 Sep 26 '13 at 04:37
  • and I also wrote it in
      block
    – user2659088 Sep 26 '13 at 04:38
  • You'll have to explain your for loop issue better - what do you have in it that's making you think it looks ok? If you're using `alert` like you did in checking your response, there's a huge difference between `alert` and setting an element's innerHTML - namely, one isn't processed by the web client's renderer, while the other is. – Scott Mermelstein Sep 26 '13 at 04:47
  • @user2659088: `
    ` blocks can still contain other HTML. For example, it must process `` or `` tags. Therefore `` will be interpreted as a HTML tag. Scott gave you the solution - convert `<` to `<`. See the link he gave you.
    – slebetman Sep 26 '13 at 05:26
  • @user2659088 The `'<'` is still there, it's just invisible. The browser thinks it is starting an element with the tag-name `'stdio.h'`, which it doesn't understand. Note that `'
    '` is a formatting context -- nothing more.
    – Kevin A. Naudé Sep 26 '13 at 05:42
  • oh. I think that was the problem. I get correct text if I use innerText instead of innerHTML. thank you. – user2659088 Sep 26 '13 at 06:16
0

example code was like this:

#include<stdio.h>

int main(){
    int i;
    for(i=0; i<10; i++){
        printf("aa");
    }
}

and I split it into lines, and array only didn't contain <stdio.h>.

the only problem was <stdio.h>. '<' character in for loop condition, it was visible.

I'll check converting problem again later.

user2659088
  • 133
  • 1
  • 7
  • Ah, I finally see what you're saying. You're saying the < in the for loop didn't disappear. In this case, the parser perceives an unclosed tag for a `10` element, and just renders "the opening tag" and everything after it as text. If you had a > anywhere lower in your code, it would be a different story, though the parser may catch on from the sheer number of syntax errors it generates (since not everything after the 10 would be in attribute syntax) that it was intended as a literal as well. – Scott Mermelstein Sep 26 '13 at 14:13