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?

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

6 Comments

but i can find '<' characters in for loop or other lines. I dont know why split function doesnt work..
and I also wrote it in <pre> block
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.
@user2659088: <pre> blocks can still contain other HTML. For example, it must process <b> or <span> tags. Therefore <stdio.h> will be interpreted as a HTML tag. Scott gave you the solution - convert < to &lt;. See the link he gave you.
@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 '<pre>' is a formatting context -- nothing more.
|
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.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.