6

I'm loading pages asynchronously with the jQuery load function, like this:

tree.click(function() {
                if ($(this).hasClass("file")) {
                    tree.removeClass("selected");
                    $(this).addClass("selected");
                    content.load("content/"+this.id+".html");
                    contentContainer.effect("highlight");
                    SyntaxHighlighter.all();
                }                         
            });

One of the external pages looks like this:

<pre class="brush: java;">
   /**
     * The HelloWorldApp class implements an application that
     * simply prints "Hello World!" to standard output.
     */
   class HelloWorldApp {
      public static void main(String[] args) {
         System.out.println("Hello World!"); // Display the string.
      }
   }
</pre>

now the SyntaxHighlighter.all(); call in the tree.click() function from above should render the pre block with pretty syntax highlighting, but when loading the file with the pre block via the jQuery load() function this doesn't work.

When I hardcode the pre block into the content div of the main file, it does work, however.

Any ideas??

Thanks for the answers so far . I understand the callback part and I implemented the SyntaxHighlighter.all() call in the callback of the load function now, but still no luck...

I'll append the 2 complete files, as they are rather small in size atm.

index.php:

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>braindump</title>
    <link type="text/css" href="css/style.css" rel="stylesheet" />
    <link type="text/css" href="css/jquery.treeview.css" rel="stylesheet" />
    <script type="text/javascript" src="jquery/jquery-1.3.2.min.js"></script>
    <script type="text/javascript" src="jquery/jquery-ui-1.7.2.custom.min.js"></script>
    <script type="text/javascript" src="jquery/jquery.treeview.js"></script>
    <script type="text/javascript" src="syntaxhighlighter_2.0.320/scripts/shCore.js"></script>
    <script type="text/javascript" src="syntaxhighlighter_2.0.320/scripts/shBrushJava.js"></script>
    <link type="text/css" rel="stylesheet" href="syntaxhighlighter_2.0.320/styles/shCore.css"/>
    <link type="text/css" rel="stylesheet" href="syntaxhighlighter_2.0.320/styles/shThemeDefault.css"/>
    <script type="text/javascript">
        $(document).ready(function() {
            var tree = $("#tree li");
            var contentContainer = $("#contentContainer");
            var content = $("#content");

            SyntaxHighlighter.config.clipboardSwf = 'syntaxhighlighter_2.0.320/scripts/clipboard.swf';
            SyntaxHighlighter.all();

            // Treeview
            $("#tree").treeview({
                persist: "location",
                collapsed: true
            });

            tree.click(function() {
                if ($(this).hasClass("file")) {
                    tree.removeClass("selected");
                    $(this).addClass("selected");
                    content.load("content/"+this.id+".html", function() {
                        contentContainer.effect("highlight");
                        SyntaxHighlighter.all();
                    });
                }                         
            });

        });
    </script>
</head>
<body>
    <div id="container">
        <div id="header">

        </div>

        <div id="leftMenu" class="thinDottedBorder">
            <div class="min500px"></div>
            <ul id="tree" class="filetree">
                <li>
                    <span class="folder selectable">Design Patterns</span>
                    <ul>
                        <li id="softwareengineering/designpatterns/decorator" class="file"><span class="file selectable">Decorator Pattern</span></li>
                        <li id="softwareengineering/designpatterns/visitor" class="file"><span class="file selectable">Visitor Pattern</span></li>
                        <li id="softwareengineering/designpatterns/chainofresponsibility" class="file"><span class="file selectable">Chain of Responsibility</span></li>
                    </ul>
                </li>
                <li>
                    <span class="folder selectable">Design Principles</span>
                    <ul>
                        <li></li>
                    </ul>
                </li>
            </ul>
            <div class="clear"></div>
        </div>

        <div id="contentContainer" class="thinDottedBorder">
            <div class="min500px"></div>
            <div id="content">
                <pre class="brush: java;">
/**
* The HelloWorldApp class implements an application that
* simply prints "Hello World!" to standard output.
*/
class HelloWorldApp {
   public static void main(String[] args) {
      System.out.println("Hello World!"); // Display the string.
   }
}
</pre>
            </div>
            <div class="clear"></div>
        </div>
    </div>
</body>

and the other file:

<pre class="brush: java;">
 /**
 * The HelloWorldApp class implements an application that
 * simply prints "Hello World!" to standard output.
 */
 class HelloWorldApp {
    public static void main(String[] args) {
       System.out.println("Hello World!"); // Display the string.
    }
  }
</pre>

please note that initially the hardcoded pre block is rendered correctly by SyntaxHighlighter.all(), but the one in the callback of the load function doesn't work.

nkr1pt
  • 4,691
  • 5
  • 35
  • 55
  • at the risk of sounding like an idiot, did you mean contentContainer.effect("highlight"); or content.effect("highlight"); – karim79 Sep 02 '09 at 23:46
  • does the call to highlight happen before the content is loaded? e.g. would it be better as a callback onload of the content? – scunliffe Sep 02 '09 at 23:47

2 Answers2

25

SyntaxHighlighter.all ties into the window.onload event - which only fires once.

To syntax-highlight after the page loads, use the highlight function instead:

content.load("content/"+this.id+".html", function () {
  // this is executed after the content is injected to the DOM
  contentContainer.effect("highlight");
  SyntaxHighlighter.highlight();
});

Fingers crossed that works, if not (based on looking at the code) you might need to chuck in some explicit arguments (where {} is an empty set of configuration parameters, and this will be content when called from the ajax load handler):


  SyntaxHighlighter.highlight({}, this);
searlea
  • 8,173
  • 4
  • 34
  • 37
  • sorry for the push, i just tryed Syntaxhighlighter.highlight(); and it works fine! but i get an alert() whenerver a text is added, saying: brush js not found or something. can i disable this message? – Philipp Spiess Mar 13 '11 at 16:15
0

You need to call that in the callback to load:

  content.load("content/"+this.id+".html",function() {
     SyntaxHighlighter.all();
  });

load is asynchronous so it happily continues along executing statements while the GET request is done in the background.

seth
  • 36,759
  • 7
  • 60
  • 57