Search code examples
javascriptjqueryajaxgoogle-code-prettify

Javascript prettyPrint an external java file


I'm really new to web development and I'm kind of lost here.

I'm using Bootstrap, and I'm trying to display java code (in a file called test.java) that's on my local machine on the webpage. The file is displayed, but it doesn't get syntax coloured. Please help!

I have in the header:

for prettify:

<link rel="stylesheet" type="text/css" href="../localfile/prettify.css"/>
<script src="https://google-code-prettify.googlecode.com/svn/loader/run_prettify.js"></script>
<script src="../localfile/prettify.js"></script>

and for jQuery:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

And this is the script

<script type="text/javascript">
 $(document).ready(function() {
     jQuery(function($) {
         $.get('test.java', function(data) {
             $('#sourceCodeDestination').html(data);
             prettyPrint();
         }, "text");
     });
 });
</script>

and this for the div:

<div class="panel-body" >
       <pre id="sourceCodeDestination" class="prettyprint linenums lang-java">
        </pre>
</div>

Solution

  • I finally figured out what the problem was after reading the answer to this question, here.

    So, I changed the script to this and it works just fine. I just removed the prettyprinted class before calling prettyPrint.

        <script type="text/javascript">
    
          jQuery(function ($) {  $.get('test.java', function(data) {
          $('#sourceCodeDestination').html(data);
          $('#sourceCodeDestination').removeClass("prettyprinted");
            prettyPrint();  
        }, "text");
     });
    
    </script>