很抱歉,这几乎肯定是重复的,但我一直在搜索StackOverflow并且无法找到答案。希望如果我用最一般的术语来表达它,它将来会帮助别人。
我在我的网页中包含一些脚本,这些脚本在Chrome / FF / Opera / Safari / IE9中非常出色,但与IE8或以下版本不兼容(对于信息,它是非常棒的D3.js)。 / p>
如何使用确保页面在IE< 9?
中正常失败理想情况下,我更喜欢IE< 9用户
HTML页面中的当前代码:
<script type="text/javascript" src="/js/d3.js"></script>
<script type="text/javascript" src="/js/mylocal.js"></script>
在JavaScript文件中,从开头行开始:
var vis = d3.select("#vis")
.append("svg:svg")
.attr("width", w)
.attr("height", h)
.append("svg:g");
// etc
我应该如何更改这些以确保只有支持D3的浏览器才会下载并尝试执行它?
答案 0 :(得分:6)
您应该使用IE conditional comments:
<!--[if lt IE 9]>
<p>Sorry, this won't work for you</p>
<![endif]-->
<!--[if gt IE 8]><!-->
<script type="text/javascript" src="/js/d3.js"></script>
<script type="text/javascript" src="/js/mylocal.js"></script>
<!--<![endif]-->
答案 1 :(得分:0)
function getIEVersion(){
var ua = window.navigator.userAgent;
var msie = ua.indexOf ("MSIE ");
if (msie > 0)
return parseInt(ua.substring(msie+5, ua.indexOf (".", msie ))); // If Internet Explorer, return version number
else
return 10; // If another browser, return 10
}
if(getIEVersion() < 9 ){
alert("Sorry, this won't work for you");
}else{
<script type="text/javascript" src="/js/d3.js"></script>
<script type="text/javascript" src="/js/mylocal.js"></script>
}