这似乎仍然不是直截了当的。
http://www.w3.org/TR/MathML2/chapter8.html
可以通过使用测试字符串“org.w3c.dom.mathml”调用 DOMImplementation::hasFeature 方法来查询对 MathML 文档对象模型的支持
这意味着一个简单的测试,但是 Chrome 和 IE 通过插件支持这一点,但即使没有插件,Chrome 也会返回true
我的解决方案是使用 w3c 规范,但适用于浏览器 [chrome] 必须做出相反响应的情况。然后我可以在必要时使用 MathJax,除了 Firefox 之外,它总是如此。脚本进入 html <head> 部分
<script type="text/javascript">
//Browser detector for Chrome
//returns true if the Browser is Chrome
function isChrome(){
var regex = /Chrome\/[0-9]{1,2}\.[0-9]/
var matches = navigator.userAgent.match(regex)
//console.log( matches )
return (matches!=null && matches.length==1)
}
/*
* Feature Detect for MathML as w3c specification
* <returns>boolean: true if mathML is supported in browser
*/
function hasFeatureMathML(){
MATHML_FEATURE = "org.w3c.dom.mathml" //as per w3c specification
MATHML_FEATURE_VERSION = "2.0" //Any version number appears to work
if(isChrome()) return false //Opps Chrome not natively supported yet
return document.implementation.hasFeature(MATHML_FEATURE, MATHML_FEATURE_VERSION )
}
/*
* init MathML use MathJax according to
* http://docs.mathjax.org/en/latest/dynamic.html
* with additional test to confirm necessity
* <returns>boolean: true if mathML is supported in browser
*/
function initMathML(){
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML";
//doctorBob added test on next line, return if has native support for MathML
if( hasFeatureMathML() ) return true
document.getElementsByTagName("head")[0].appendChild(script)
return false
}
//initialize in html <head> incase MathJax is required
var browserHasMathML = initMathML()
if( !browserHasMathML )console.log("No Native MathML using MathJax")
</script>
我并没有真正考虑安装浏览器插件,因为不是每个人都安装了它们。这适用于 IE 8、Chrome 39、Firefox 38、Komodo Edit 6