Prism.js Disturbs Ajax?

176 views Asked by At

I have two simple Python code files hello.py and world.py.

print("hello")
print("world")

Based on this way, I wrote, using both Prism.js and Ajax, an HTML file helloworld.htm that formats and presents either hello.py or world.py at a time as follows.

<!doctype html>
<script src=https://cdnjs.cloudflare.com/ajax/libs/prism/1.25.0/prism.min.js></script>
<script src=https://cdnjs.cloudflare.com/ajax/libs/prism/1.25.0/plugins/line-numbers/prism-line-numbers.min.js></script>
<script src=https://cdnjs.cloudflare.com/ajax/libs/prism/1.25.0/components/prism-python.min.js></script>
<link rel=stylesheet href=https://cdnjs.cloudflare.com/ajax/libs/prism/1.25.0/themes/prism.min.css>
<link rel=stylesheet href=https://cdnjs.cloudflare.com/ajax/libs/prism/1.25.0/plugins/line-numbers/prism-line-numbers.min.css>
<link rel=stylesheet href=https://cdnjs.cloudflare.com/ajax/libs/prism/1.25.0/themes/prism-coy.min.css>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript">
    function btnclick(_url){
        $.ajax({
            url : _url,
            type : 'post',
            success: function(data) {
             $('#DIVID').html(data);
            },
            error: function() {
             $('#DIVID').text('An error occurred');
            }
        });
    }
</script>
<div style="width:50%;height:100%;position:fixed;top:0;left:0">
<a href="cursor:pointer" onclick="btnclick('hello.py')">hello.py</a><br>
<a href="cursor:pointer" onclick="btnclick('world.py')">world.py</a>
<div style="width:50%;height:100%;position:fixed;top:0;left:50%">
<pre class=line-numbers><code class=lang-py><div id="DIVID"><?php include('hello.py');?></div></code></pre>

Unfortunately, this HTML file does not work correctly with <code class=lang-py> in the last line. Though <code> instead of <code class=lang-py> solves this problem, I still want to use Prism.js to format the code files. How can I address this issue?

P.S. It seems that this problem is related to How to Re-Run Prism.js on AJAX Content and that Prism.highlightAll(); needs to be run together with Ajax, though I could not find the correct location of Prism.highlightAll(); yet.

0

There are 0 answers