如何从javascript检测MathML标记支持(<;mfrac>;,<;mtable>;)?

如何从javascript检测MathML标记支持(<;mfrac>;,<;mtable>;)?,javascript,mathml,Javascript,Mathml,我可以通过以下方式检测MathML支持: var e = document.createElement('div'); e.innerHTML = '<math></math>'; var mathMLSupported = e.firstChild && "namespaceURI" in e.firstChild && e.firstChild.namespaceURI == 'http://www.w3.org/199

我可以通过以下方式检测MathML支持:

var e = document.createElement('div');
    e.innerHTML = '<math></math>';
    var mathMLSupported = e.firstChild && "namespaceURI" in e.firstChild && e.firstChild.namespaceURI == 'http://www.w3.org/1998/Math/MathML';
var e=document.createElement('div');
e、 innerHTML='';
var mathMLSupported=e.firstChild&&e.firstChild.namespaceURI=='http://www.w3.org/1998/Math/MathML';
但是,如何检测对
的支持?

以下一致性浏览器必须为DOM中的特定MathML元素实现几个属性(又称绑定)。因此,您只需创建一个MathML mtable元素,并检查浏览器是否添加了
rowalign
属性:

var tmp = document.createElementNS('http://www.w3.org/1998/Math/MathML',
                                   'mtable');
if (tmp.hasOwnProperty('rowalign')) {
  return true;
} else {
  return false;
}

在中,我构造了一个隐藏的
元素,并将其计算高度与非分数的高度进行比较。有关实际代码,请参见jqmath-0.1.js中的M.checkMathML函数。尝试使用或不使用XML名称空间(取决于浏览器),并为Internet Explorer的MathPlayer插件提供名称空间前缀,这有点复杂。

这似乎还不是很简单

可以通过使用测试字符串“org.w3c.dom.MathML”调用doImplementation::hasFeature方法来查询对MathML文档对象模型的支持

这意味着一个简单的测试,但是Chrome和IE通过插件支持,但是Chrome即使没有插件也会返回true

我的解决方案是使用w3c规范,但适用于浏览器[chrome]必须做出相反响应的情况。然后,如果必要的话,我可以使用MathJax,除了firefox之外,它一直都是。脚本位于html部分

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

//铬浏览器检测器
//如果浏览器为Chrome,则返回true
函数isChrome(){
var regex=/Chrome\/[0-9]{1,2}\[0-9]/
var matches=navigator.userAgent.match(regex)
//console.log(匹配项)
返回(匹配项!=null&&matches.length==1)
}
/*
*MathML作为w3c规范的功能检测
*布尔值:如果浏览器中支持mathML,则为true
*/
函数hasFeatureMathML(){
MATHML_FEATURE=“org.w3c.dom.MATHML”//符合w3c规范
MATHML_FEATURE_VERSION=“2.0”//任何版本号都可以正常工作
if(isChrome())返回false//Opps Chrome本机尚不支持
return document.implementation.hasFeature(MATHML\u功能,MATHML\u功能版本)
}
/*
*init MathML根据
*     http://docs.mathjax.org/en/latest/dynamic.html
*附加测试以确认必要性
*布尔值:如果浏览器中支持mathML,则为true
*/
函数initMathML(){
var script=document.createElement(“脚本”);
script.type=“text/javascript”;
script.src=”http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML”;
//doctorBob在下一行添加了测试,如果对MathML有本机支持,则返回
如果(hasFeatureMathML())返回true
document.getElementsByTagName(“头”)[0].appendChild(脚本)
返回错误
}
//在需要MathJax的情况下用html初始化
var browserHasMathML=initMathML()
if(!browserHasMathML)console.log(“没有使用MathJax的本机MathML”)
我没有真正考虑安装浏览器插件,因为不是每个人都安装了它们。 这适用于IE 8、Chrome 39、Firefox 38、Komodo Edit 6和
元素#getBoundingClientRect
函数hasMathMLSupport(){
const div=document.createElement(“div”);
div.innerHTML='';
文件.正文.附件(div);
返回div.firstChild.firstChild.getBoundingClientRect().height==20;
}

log(hasMathMLSupport())您需要在HTML标记周围放置'qoutes,否则它们将被过滤掉。:)我需要真实的结果,因为这不起作用。它在不支持MathML的Chrome中返回true。看来Firefox没有提供任何建议的绑定。也就是说,你运气不好。您无法将其与MathML命名空间中任意命名的元素区分开来。这与MathJax和Modernizer的做法基本相同: