简体   繁体   中英

(google prettify) syntax highlighting not working after call back

The method work fine with single php file:

So in my html page I try to use js and call php file and highlighting the content by google prettify

<!DOCTYPE html>
<html lang="en">
<head>
<link href="googleprettify/prettify.css" type="text/css" rel="stylesheet" />

<script src="googleprettify/prettify.js" type="text/javascript"></script>   
<script>
function loadXMLDoc()
{
var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("myDiv").innerHTML=xmlhttp.responseText;


    }
  }
xmlhttp.open("GET","callback_json.php",true);
xmlhttp.send();

}
</script>
   </head>

  <body onload="prettyPrint()" >

  <button onclick="loadXMLDoc();">Get Data</button>
<div id="...">
<pre class='prettyprint;'> 
    function foo()
    {
        if (counter == 10)
            return;
        // it works!
    }</pre>
</div>



<div id="myDiv">
need to be replaced
</div>
 </body>

</html>

The first piece of code works just fine but when i call php file

<?php
  $ch = curl_init();
  $url='someurl';
  $timeout = 5;
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  $data = curl_exec($ch);
  curl_close($ch);

  echo '<pre class="prettyprint;"> ';
  echo $data;
  echo "</pre>";?>

And the returning code can't be highlighted. I doubt there's problem with body onload="prettyPrint()" since the prettyprint() only run when the page loaded but I'm not sure about this. Or is there any problem with the code structures? Or is there anyway to load prettyprint function again without loading the whole page?

After you update the DOM, call prettyPrint() again, otherwise you only have inserted some new HTML, but the prettification did not run:

...
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
      document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
      prettyPrint();
      ^^^^^^^^^^^^^^

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM