I'm using prettyprint plugin as syntax highlighter, it work fine when page loads but when i add new elements dynamically it doesn't work! I tried using prettyPrint()
to invoke it after loading the new contents but it didn't work! i also followed the instructions on the plugin website by wrapping prettyPrint()
with a function but it didn't work neither! any help would be much appreciated.
i installed the plugin like this:
<script src="https://google-code-prettify.googlecode.com/svn/loader/run_prettify.js"></script>
my code is:
function showCode(e){
(e.preventDefault) ? e.preventDefault() : e.returnValue = false;
var parent = document.createElement('div'),
pre = document.createElement('pre'),
code = document.createElement('code'),
elm = (e.currentTarget) ? e.currentTarget : e.srcElement,
src = elm.getAttribute('href'),
id = elm.getElementsByTagName('img')[0].getAttribute('src').replace(/images\/(.+?)\.png/g, "$1");
parent.id = "codeZoom";
pre.className = "prettyprint linenums lang-" + id;
var xhr = (window.XMLHttpRequest) ? new window.XMLHttpRequest() : new activeXObject("Microsoft.XMLHTTP");
xhr.open('get', src, true);
xhr.send();
xhr.onreadystatechange = function()
{
if(xhr.readyState == 4 && xhr.status == 200)
{
var text = document.createTextNode(xhr.responseText);
code.appendChild(text);
pre.appendChild(code);
parent.appendChild(pre);
document.getElementsByTagName('body')[0].appendChild(parent);
center(parent);
prettyPrint();
}
}
}
currently i'm getting the error message prettyPrint is not defined.
prettyPrint
is not defined then it is because it is not defined! Impossible to know with your current code – ConfessionprettyPrint
function when page loads which i think it does because it works on otherpre
elements when page starts! – SalternprettyPrint
function by using the undocumentedPR. prettyPrint()
function. Although, it is safer if you stop using the loader and useprettyPrint()
instead. Let me know if this is your case – ConfessionPR.prettyPrint()
is not a function – Saltern