load Mathjax after loading javascript

若如初见. 提交于 2019-12-04 16:49:25

You can try using a JavaScript loader with a callback to load MathJax and run your code in the callback. See https://github.com/niftylettuce/javascript-async-callback for example.

EDIT:

<html>
<head>
<script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.0.min.js"></script>
<script>
function gup( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}
</script>

<script>
$(function() {
  var x = gup('x');
  var y = gup('y');
  var z = gup('z');
  var text = "This is the line that NOT show correct <-- It's OK now" + "\\[ \\frac{" + x + " + " + y + "}{" + z +"} \\]";
  document.getElementById("step1").innerHTML= text;
});
</script

</head>
<body>
<p>This is the line that load correct  \[ \frac{x+y}{z} \]</p>
<p id="step1"></p>
</body>
</html>

If you modify the document to add mathematics after MathJax has run, you need to tell MathJax to run again. You do that using

<script>
  MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
</script>

See the relvent MathJax documentation for details.

In your case, this should do it:

<script>
var x = gup('x');
var y = gup('y');
var z = gup('z');
var text = "This is the line that NOT show correct" + "\[ \frac{" + x + " + " + y + "}{" + z +"}\]";
document.getElementById("step1").innerHTML= text;
MathJax.Hub.Queue(["Typeset",MathJax.Hub,"step1"]);
</script>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!