有点具体的问题。我一直在阅读有关如何定位 ie10 及以下版本的信息,但确实没有找到适合我的情况的具体答案。
我的网站使用的框架不能很好地支持 ie10(谷歌的 polymer ),因此当用户使用 ie10 及以下版本时,我设置了一个备用页面。我遇到的问题是,当我在 ie10 中打开网页时,让 polymer 运行 platform.js
所需的资源导致了如此多的错误,以至于以某种方式阻止了我的其余资源的加载。 platform.js
位于我的加载顺序的顶部,并且我不是异步加载的。我试图在一个 js 文件中定位 ie10,该文件必须在 platform.js
下加载,但由于 platform.js
导致如此多的问题,我的代码根本没有注册错误。
我想知道是否有一种方法可以仅在浏览器为 !ie10
时加载 platform.js
,同时仍将其放在加载顺序的顶部。呃,这让我发疯。即使是一个指向我可以阅读如何使我的资源仅在某些事件上加载的链接也会有所帮助。感谢您提供任何信息!
最佳答案
我终于找到了一种方法来使用 Mario's answer here 来做到这一点,以防将来有人感兴趣:
我在我的index.html文件的头部开始处弹出了这个:
<script>
function detectIE() {
var ua = window.navigator.userAgent;
var msie = ua.indexOf('MSIE ');
var trident = ua.indexOf('Trident/');
if (msie > 0) {
// IE 10 or older => return version number
return parseInt(ua.substring(msie + 5, ua.indexOf('.', msie)), 10);
}
if (trident > 0) {
// IE 11 (or newer) => return version number
var rv = ua.indexOf('rv:');
return parseInt(ua.substring(rv + 3, ua.indexOf('.', rv)), 10);
}
// other browser
return false;
}
// making sure it's working..
console.log(detectIE());
// this is important: if you just say <= 10 then it will also accept ZERO
// and ZERO is = false, which makes good browsers like Chrome detect as IE
if (detectIE() >= 1 && detectIE() <= 10) {
console.log('this is below ie10');
} else {
// add my script for polymer
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "/bower_components/platform/platform.js";
//put it at the start of the head tag
document.getElementsByTagName('head')[0].appendChild(script);
}
</script>
我注意到网站首次加载时有时会出现一点延迟,但这还不错。直到我能够更好地使用 Modernizr 之类的东西之前,我必须这样做。
关于javascript - ie10及以下如何防止资源被加载?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26250461/