user1204111
user1204111

Reputation:

Load and append external html file

I want to load external HTML file with a content like this:

{"html":" \n <font>\u043b\u0432.<\/font> \n ","back":""}

I tried with this code - it loads the file but it remains with \n and the other things..

<div id="success"></div>
<script>$.get('test.html', function(data) { $(data).appendTo("#success"); } );</script>

Upvotes: 2

Views: 1865

Answers (2)

Mamsaac
Mamsaac

Reputation: 6273

Instead of using appendTo, do the following:

<div id="success"></div>
<script>$.get('test.html', function(data) { $("#success").html(data); } );</script>

Please beware: If you don't control the contents of "data", you're exposing yourself to XSS attacks. If you do, it is ok.

Upvotes: 0

Anoop
Anoop

Reputation: 23208

Modified code: Since your data is a JSON object you should use $.getJSON . and replace all \n with <br>.

<script>
   $.getJSON('test.html', function(data) {
      data =data.html.replace(/\n/g, "<br>"); 
      $(data).appendTo("#success"); } );
</script>

Upvotes: 1

Related Questions