2012-09-27 76 views
2

這裏以後就是我的簡單HTML代碼負載Mathjax加載JavaScript

<html> 
<head> 
<script type="text/javascript" 
src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"> 
</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> 
</head> 
<body> 
<p>This is the line that load correct \[ \frac{x+y}{z} \]</p> 
<p id="step1"></p> 
<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; 
</script> 
</body> 
</html> 

當我加載此HTML文件和URL一樣

sample.html?x=1&y=2&z=3 

的第一句話顯示正確的形式和負載Mathjax但發送參數第二句話不是。這是因爲Mathjax之前加載了java腳本代碼。你知道如何在Javascript之後加載Mathjax

+1

注:

在你的情況,這應該這樣做cdn.mathjax.org接近其結束生命,檢查https://www.mathjax.org/cdn - 關閉遷移提示(也許更新您的文章,供未來的讀者閱讀)。 –

回答

2

您可以嘗試使用帶有回調的JavaScript加載器加載MathJax並在回調中運行代碼。例如, 參見https://github.com/niftylettuce/javascript-async-callback

編輯:

<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> 
+0

不適合我。 – nfarshchi

+0

究竟誰沒有工作?它可能會拋出一些錯誤?請粘貼您編寫的代碼來測試它。 – alexandernst

+0

我在頭文件>後添加了這一行,並在body部分添加了javascript部分但不前夕告訴我 「警告」 沒有我在錯誤的地方插入? – nfarshchi

2

如果修改了文檔MathJax運行後添加數學,你需要告訴MathJax再次運行。你這樣做使用

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

有關詳細信息,請參閱relvent MathJax documentation。從未來

<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>