Search code examples
htmliframepre

How can i insert into my website a HTML source code without being modified?


In a section of my webpage is displayed a source code of a HTML (with a <body>, a <head>, ALL that can have a HTML code), and the user can extract the code. I have that code in a text file into a iframe. The problem: Iframe doesn't works in browsers like IE8, because in modern browsers like Chrome, they insert a <pre> within the iframe, and IE8 doesn't.

I need a way for insert the code in the document, without the browser execute the code. Yes, there isn't necessary that the code must be in a text file (.txt), i have seen some people that set a code within a <textarea> and i like that. The idea is that the source code must not be modified, and i have read some PHP solutions, but i can't modify the PHP. I have tried with a <pre> and a <code>, but.. doesn't works, the browser execute the source code anyway, maybe i don't understand the correct use of that tags.

I have read that sometimes is necessary replace some special characters like < for &#60;, could be that the problem? exist a converter that will not affect the final result when be displayed? Thanks! I don't know if i explain me good, i am open to any suggestion (sorry if i sound weird, english is not my native language)


Solution

  • With jQuery, you can use AJAX to fetch the file with the source code, and then use the .text(var_with_the_code) in a jQuery object to display HTML source code without "executing" it.

    Here is a limited demo in jsFiddle: http://jsfiddle.net/gberger/T3JDJ/