简体   繁体   English

跳过包含用于 Internet Explorer 的 d3.js

[英]Skip inclusion of d3.js for Internet Explorer

I have a project that uses the latest version of d3 (v6), I include the api as follows in my index.html:我有一个使用最新版本 d3 (v6) 的项目,我在 index.html 中包含了 api,如下所示:

        <script src="scripts/d3.js"></script>

This version of d3 does not support IE, I get a console error as the ECMA version of Javascript in IE does not support => for example.此版本的 d3 不支持 IE,例如,我收到控制台错误,因为 IE 中的 Javascript 的 ECMA 版本不支持 =>。

This is fine for me as I test if window.d3 exists and if not then I show something else instead of my d3 chart.这对我来说很好,因为我测试window.d3是否存在,如果不存在,那么我会显示其他内容而不是我的 d3 图表。

But I would like to not have this console error.但我不想有这个控制台错误。

So how do I only include d3.js if the browser is not IE?那么如果浏览器不是IE,我如何只包含d3.js?

Thanks谢谢

You can check the browser userAgent to check if it is IE, if not then load the d3 script.您可以检查浏览器userAgent以检查它是否为 IE,如果不是则加载 d3 脚本。

You can refer to the code below, it works well and clear d3 console error in IE 11:您可以参考下面的代码,它运行良好并清除了 IE 11 中的 d3 控制台错误:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title></title>
    <script>
        var ua = window.navigator.userAgent;
        var msie = ua.indexOf("MSIE ");
        if (!(msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./))) //If not IE, load d3 script
        {
            document.write('<script src="scripts/d3.js"><\/script>');
        } 
    </script>
</head>
<body>
    <svg>
        <circle class="target" style="fill: #69b3a2" stroke="black" cx=50 cy=50 r=40></circle>
    </svg>
    <script>
        if (window.d3) {
            d3.select(".target").style("stroke-width", 8);
        }
        else {
            alert("IE");
        }
    </script>
</body>
</html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM