I have the following page (css useless--omitted):
HTML:
<!DOCTYPE html>
<html lang=en>
<head>
<meta charset="UTF-8">
<title>Command Line</title>
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
extensions: ["http://cs.jsu.edu/mathjax-ext/contrib/forminput/forminput.js"],
tex2jax: {inlineMath: [['$','$'], ['\\(','\\)']]},
TeX: {extensions: ["AMSmath.js","AMSsymbols.js"]}
});
</script>
<script type="text/javascript" src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script src="!!IMPORTANT_JS!!.js"></script>
<script src="theta.js"></script>
<script src="cmd.js"></script>
<link rel="STYLESHEET" type="text/css" href="styles.css">
</head>
<header>
<h1 id="title">COMMAND LINE v.2.0</h1><span id="copy">© 2015 ~ Conor O'Brien</span>
</header>
<body>
<div id="subtitle">Now with $\LaTeX$!</div>
<div id="out"></div>
<h3 id="sQ"></h3>
</body>
<footer>
<input type="text" id="cmd">
<input type="color" id="col">
<div id="left">>></div>
</footer>
</html>
JavaScript:
$( document ).ready(function(){
window.t = false;
$("#col").on("change",function(){
var d = $q(this).value;
$q("#out").innerHTML+="<p>Color: <span style=\"color: "+d+";\">"+d.toUpperCase()+", "+Color.hexToRgb(d).v+" - "+linkC("/color "+d,"Use")+"</span>.</p>";
});
$("#cmd")[0].addEventListener("keypress",function(e){
if(e.key=="Enter"){
submit();
}
});
$("#out").html("Initializing...");
window.init = function(){
var d = [br(3)+
"********* INITIALIZED *********",
"* Welcome to the experimental *",
"* JavaScript command line! It *",
"* will execute exactly like a *",
"* handheld calculator, having *",
"* the capacity to execute the *",
"* user's inputs. *",
"* *",
"* ~-~-~-~-~-~-~-~-~-~-~-~-~-~ *",
"* *",
"* Type 'help' for environment *",
"* commands. *",
"*******************************"+br(),
"<hr>"],i=0;
function x(d,i){
$q("#out").innerHTML += (d[i++])||"";
$q("#out").innerHTML += br();
if(i<d.length){ setTimeout(x,20,d,i); } else {
window.t = true;
}
}
setTimeout(x,500,d,i);
}
init();
});
function br(x){
var o = "",x=x||1;
for(i=0;i<x;i++){
o+="<br>";
}
return o;
}
function $q(i){
return $(i)[0];
}
function submit(){
if(t){
var $out = getOutput();
if($out){
var d = "<p id='a"+(el++)+"'>"+$out+"</p>";
$q("#out").innerHTML += d;
lines.push(d);
marker = lines.length;
location = "file:///C:/Users/Conor%20O%27Brien/Documents/Programming/command%20line%20v.2.0/main.html#sQ";
$q("#cmd").focus();
console.log(el);
MathJax.Hub.Queue(["Typeset",MathJax.Hub,"a"+el+""]);
}
}
}
var pi = Math.PI;
var e = Math.E;
function Reval(str){
with(Math){var tstr = str.replace(/(.+)\^(.+)/g,"Math.pow($1,$2)");return eval(tstr);}
}
function Dval(str){
return "$"+str.replace(/(.+)\^(.+)/g,"{$1}^{$2}")+"$";
}
var el = 0;
function getOutput(){
var val = $q("#cmd").value;
if(val=="help"){
$q("#out").innerHTML = "";
return "<h1>HELP</h1><h2>Commands</h2>"+
[
"= help",
" - displays help document.",
"= open {target}",
" - displays {target}'s contents.",
" - {target} values:",
" * options: adjust options with subpoints (i.e. options.{prop} = {1/0} [1=True,0=False])"
].join(br())+"<hr>";
} else if(val=="cls"||val=="clear"){
$q("#out").innerHTML = "";
return "Cleared.";
} else if(val.substr(0,5)=="open "){
var target = val.substr(5,val.length);
console.log(target);
switch(target){
case "options":
break;
default:
return new Error(target+" is not a valid target!");
break;
}
} else {
try {
console.log(val,Reval(val));
return Dval(val)+br()+"<span class='ans'>"+Reval(val)+"</span>";
} catch(e) {
return e;
}
}
}
Upon executing, this (ideally) will able to be evaluate the user's input (i.e.
sin(3)
or 3 + 5^3 / 99.3
). This code does this successfully, though displaying it is trickier. I am using MathJax to display the math correctly, and using MathJax.Hub.Queue(["Typeset",MathJax.Hub,"a"+el+""]);
to target the element a{number}
and redo its MathJax. This somehow refreshes the entire page and furthermore, after a few entries into the input, will start to duplicate the previous equations.
I have narrowed it down to what I believe would be the solution: MathJax has two observable phases.
- It formats the equations, then
- It applies fonts.
The MathJax function only seems to perform these two steps in my code twice: once, at the beginning, and once, when the function is first executed.
My question is, how can I fix this? I've been trying to fix this, but to no avail.