加载 jQuery 之前运行的增强.js 库 onScriptLoaded
请使用 Filament Group 的渐进式增强功能查看此演示enhance.js 库
修改为:-
<script type="text/javascript" src="../_shared/EnhanceJS/enhance.js"></script>
<script type="text/javascript">
// Run capabilities test
enhance({
loadScripts: [
{src: 'js/excanvas.js', iecondition: 'all'},
'../_shared/jquery.min.js',
'js/visualize.jQuery.js',
'js/example.js'
],
loadStyles: [
'css/visualize.css',
'css/visualize-dark.css'
]
});
alert($);
</script>
我在警报中收到“$ is undefined error”,大概是因为 jQuery 有尚未加载。
注意 - 警报($)只是为了调试 - 我正在尝试用它做一些事情,例如 $('some-selector') - 并且没有 $(handler) 或 $(document).ready 等要么工作 - jquery 未加载,$ 未定义。
增强的 onScriptLoaded 看起来应该可以解决这个问题,但是如果我添加进去
onScriptsLoaded:[runMyCode()]
,稍后
function runMyCode()
{
alert("runMyCode was called");
alert($);
}
第一个警报会起作用,但 $ 仍然没有定义。
如果我在按钮的单击事件上调用 runMyCode 它确实有效。
所以问题是 - 一旦所有脚本都完成使用enhance.js 的加载,如何运行代码?
PS 在 IE8/FF3/Chrome7 中验证了相同的问题
See this demo from Filament Group using their Progressive Enhancement enhance.js library
Modifying to give :-
<script type="text/javascript" src="../_shared/EnhanceJS/enhance.js"></script>
<script type="text/javascript">
// Run capabilities test
enhance({
loadScripts: [
{src: 'js/excanvas.js', iecondition: 'all'},
'../_shared/jquery.min.js',
'js/visualize.jQuery.js',
'js/example.js'
],
loadStyles: [
'css/visualize.css',
'css/visualize-dark.css'
]
});
alert($);
</script>
I get an "$ is undefined error" on the alert presumably because jQuery has not loaded yet.
Note - the alert($) is just for debugging - I am trying to do something with that e.g. $('some-selector') - and no $(handler) or $(document).ready etc don't work either - jquery is not loaded, $ is not defined.
enhance's onScriptLoaded looks like it should do he trick but if I add in
onScriptsLoaded:[runMyCode()]
and later
function runMyCode()
{
alert("runMyCode was called");
alert($);
}
The first alert works but $ is still not defined.
If I call runMyCode on a click event of a button it does work.
So the question is - how can you run code once all scripts have finished loading with enhance.js?
P.S. Same problem verified in IE8/FF3/Chrome7
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为这就是您想要的,请注意 < code>onScriptsLoaded 直接用作回调,因此它应该是一个函数,而不是数组:
或者更一般地,对于函数,仅传递引用,如下所示:
原因
onScriptsLoaded: [runMyCode()]
“有点”的作用是立即调用runMyCode
函数(在该行运行时,不 > 当脚本完成时)并根据该结果创建一个数组。当脚本完成时,这实际上会抛出一个错误,因为 onScriptsLoaded 应该是一个回调函数,而不是一个数组。I think this is what you're after, note that
onScriptsLoaded
is used directly as a callback, so it should be a function, not an array:Or more generally for a function, pass just the reference like this:
The reason
onScriptsLoaded:[runMyCode()]
"kind of" works is it's immediately invoking therunMyCode
function (at the time this line runs, not when the scripts finish) and makes an array from that result. This will actually throw an error when scripts do finish, sinceonScriptsLoaded
is expected to be a callback function, not an array.我认为这可能是一个问题:
您尝试过吗(假设数组符号是正确的):(编辑 - 它不正确)
?第一种方法是在设置库时调用函数。您只想传递对函数的引用。
编辑实际上它应该是:
我不知道数组来自哪里; “enhance.js”文档中绝对没有提到它。
I think this may be a problem:
have you tried (assuming the array notation is correct): (edit — it is not correct)
? The first way, you're calling your function at the time you're setting up the library. You want to pass just the reference to your function instead.
edit actually it should just be:
I don't know where the array came from; it's definitely not mentioned in the "enhance.js" documentation.