I have got this error while running. I don't know why is it showing that the myFunction() is not defined. Is there any problem with the way the function is loaded when the button is clicked.
This is my code here so far.
indentation.xhtml
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns = "http://www.w3.org/1999/xhtml"
xmlns:h = "http://java.sun.com/jsf/html"
xmlns:f = "http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">
<h:head>
<style type="text/css">
.code-str { color: #080;}
.code-elem { color: #f00;}
.code-comment { color: #00f;}
</style>
</h:head>
<h:body>
<h:form>
<p:inputTextarea rows="15" cols="80" id="text1"></p:inputTextarea>
<br/>
<p:commandButton type="button" value = "submit" action="indentation" onclick="myFunction()"></p:commandButton>
<div id="demo"></div>
</h:form>
</h:body>
<script type="text/javascript">
const keywords = {
IF: {style: "code-elem", indent: 4},
ENDIF: {style: "code-elem", indent: -4},
IFLISTING: {style: "code-str", indent: 4},
ENDIFLISTING: {style: "code-str", indent: -4},
VAR: {style: "code-comment", indent: 0},
LISTING: {style: "code-comment", indent: 0}
};
window.onload = function myFunction() {
let indent = 0;
document.getElementById("demo").innerHTML = document.getElementById("text1").value.split(/[\r\n]+/).map(line => {
const oldIndent = indent;
line = line.trim().replace(/###([A-Z]+)(.*?)###/g, (m, keyword, arg) => {
const param = keywords[keyword];
if (!param)
return m;
indent += param.indent;
return `<span class="${param.style}">${m}</span>`;
});
return " ".repeat(Math.min(indent, oldIndent)) + line;
}).join("<br/>");
}
</script>
</html>
Please, can anyone help me find the problem here? After pressing the button, it should provide some color to the code.