2

I have javascript code that generates XML, and calls the SyntaxHighlighter.all() method to highlight the added XML. I do not receive errors in the console but neither does highlighting take place.

javascript:

<script type="text/javascript" src="js/syntaxhighlighter_3.0.83/scripts/shCore.js"></script>
<script type="text/javascript" src="js/syntaxhighlighter_3.0.83/scripts/shBrushXml.js"></script>
<link type="text/css" rel="stylesheet" href="js/syntaxhighlighter_3.0.83/styles/shCoreDefault.css"

<script>
    function generateXML()
        {
            var result="<test>WHATEVA</test>";

            alert(result);

            var regex1 = new RegExp('<', 'g');
            var regex2 = new RegExp('>', 'g');

             result = "<pre class='brush: xml'>" + result.replace(regex1,'&lt;').replace(regex2,'&gt;')+"</pre>";

            document.getElementById("generatedXML").innerHTML = result;     
        }
</script>

HTML

    <input id="addOUTPUTButton" type="button" value="Generate XML"  onclick="generateXML()"/>
    <input id="test" type="button" value="Test"  onclick="SyntaxHighlighter.all();"/>
<div id="generatedXML">
</div>
Menelaos
  • 23,508
  • 18
  • 90
  • 155
  • Did you ever got thi resolved? I have the exact issue and appreciate if you can share your findings. – Amir Peivandi Oct 28 '13 at 16:50
  • I did solve it, though I don't remember exactly the solution. I think `SyntaxHighlighter.highlight();` instead of .all(); – Menelaos Oct 29 '13 at 13:29

2 Answers2

5

The solution is to use highlight() instead of all().

Here is a JSFiddle, note that external resources are used: http://jsfiddle.net/menelaosbgr/TXzZH/

Menelaos
  • 23,508
  • 18
  • 90
  • 155
  • This is currently broken in your example with SynatxHighlighter being undefined. This is exactly what I am seeing in the v4 build so I'm wondering what replaced "SyntaxHighlighter" identifier? – Steve Jun 21 '17 at 21:11
  • @steve see the jsfiddle, it works. This answer is for a specific version of the library when the question was asked. It is not broken- just not valid for the newer version. The new version might require a different approach. – Menelaos Jun 22 '17 at 00:36
0

Understood. I was hoping to get an answer that would also support the newer version. Either way the jsfiddle is broken so at least adding a comment mentioning version might be helpful.

Thanks for the reply.

Steve
  • 71
  • 5
  • The JSFiddle does not seem to be broken. Click Generate, and then highlight. http://jsfiddle.net/menelaosbgr/TXzZH/ . Not sure if some browser compatibility issue is at play. – Menelaos Jun 26 '17 at 13:45