通过 Modernizr.load 加载 excanvas polyfill 失败
我正在尝试在我的页面的特定于页面的 js 文件中加载 excanvas polyfill。该脚本文件插入在我的页面上的正文标记之后。
奇怪的是,如果我
<script type='text/javascript' src='/Scripts/polyfills/excanvas.compiled.js'></script>
在 head 标签中使用,一切都会很好,但如果不需要的话,我不一定想为 HTML5 兼容的浏览器加载这个脚本。
所以我很自然地尝试了使用 Modernizr 有选择地加载它。这是我完美执行但不起作用的 JavaScript 代码:
<!-- language: lang-js -->
$(function () {
Modernizr.load({
test: Modernizr.canvas,
nope: '/Scripts/polyfills/excanvas.compiled.js',
complete: function () {
setImage();
}
});
});
这似乎工作正常。 excanvas 脚本似乎已成功加载。 setImage 函数动态创建canvas 元素并将其添加到页面上的div 中。 这在 IE9 中工作正常,但在 IE8 中无法呈现。
<!-- language: lang-js -->
function setImage() {
var canvasHello = document.createElement('canvas');
canvasHello.id = 'canvasHello';
$('#divContent').append(canvasHello);
if (!Modernizr.canvas) {
G_vmlCanvasManager.initElement(canvasHello);
}
var canvasContext = canvasHello.getContext('2d');
canvasContext.width = 800;
canvasContext.height = 600;
canvasContext.fillStyle = "#000000";
canvasContext.fillRect(0, 0, 600, 800);
var img = document.createElement('img');
img.src = '/Content/images/hello.png';
img.onload = function () {
canvasContext.drawImage(img, 100, 25, 100, 100);
}
}
我是否错过了某些内容,或者 excanvas 脚本在 head 标签之外无法运行?
I'm attempting to load the excanvas polyfill in the page-specific js file for my page. This script file is inserted after the body tag on my page.
The odd bit is that if I use
<script type='text/javascript' src='/Scripts/polyfills/excanvas.compiled.js'></script>
in my head tag, everything works great, but I don't necessarily want to load this script for HTML5 compliant browsers if I don't have to.
So naturally I tried to use Modernizr to load it selectively. This is my perfectly executing, but non-functioning javascript code:
<!-- language: lang-js -->
$(function () {
Modernizr.load({
test: Modernizr.canvas,
nope: '/Scripts/polyfills/excanvas.compiled.js',
complete: function () {
setImage();
}
});
});
This seems to work fine. The excanvas script appears to load successfully.
The setImage function creates the canvas element dynamically and adds it to a div on the page.
This works fine in IE9 but fails to render in IE8.
<!-- language: lang-js -->
function setImage() {
var canvasHello = document.createElement('canvas');
canvasHello.id = 'canvasHello';
$('#divContent').append(canvasHello);
if (!Modernizr.canvas) {
G_vmlCanvasManager.initElement(canvasHello);
}
var canvasContext = canvasHello.getContext('2d');
canvasContext.width = 800;
canvasContext.height = 600;
canvasContext.fillStyle = "#000000";
canvasContext.fillRect(0, 0, 600, 800);
var img = document.createElement('img');
img.src = '/Content/images/hello.png';
img.onload = function () {
canvasContext.drawImage(img, 100, 25, 100, 100);
}
}
Did I miss something or does the excanvas script not function outside of the head tag?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在给定的要求中,您可以使用像这样的 IE 条件语句...
就足够了...
该语句只能被 IE 版本低于 9 的版本理解,并且附加脚本标记。
in the given requirement you could use the IE conditional statements like this...
would suffice....
the statement will only be understood by IE version less than 9 and the script tag gets attached.
您唯一错过的是关于如何使用 excanvas 的说明:
The only thing you missed is the instructions on how to use excanvas: