I have a C++ (.cpp) file whose contents I would like formatted on my webpage. I have it stored locally; call it myFile.cpp. The process I need is:
- Get contents of
myFile.cppstored as a JavaScript stringcodeText - Replace all of
codeText's<characters with<and all > characters with>l; - Made
codeTextthe inner HTML of<pre class="prettyprint" id="blah">(Google's code pretifier will applied inside there)
So here's my horrible attempt that isn't working:
var codeText;
var codeFile = new XMLHttpRequest();
codeFile.open("GET", "myFile.cpp", false);
codeFile.onreadystatechange = function ()
{
if(codeFile.readyState === 4)
{
console.log(codeFile.status);
if(codeFile.status === 200 || codeFile.status == 0)
{
codeText = codeFile.responseText;
}
}
}
codeFile.send(null);
codeText = AB_Escape(codeText);
document.getElementById("blah").innerHTML = codeText;
where
function AB_Escape(txt)
{
txt.replace("<","<");
txt.replace(">",">");
return txt;
}
This isn't working: my console reads Uncaught TypeError: Cannot set property 'innerHTML' of null
Hopefully that gives you an idea of what I'm trying to do. How can I do it correctly?