加载 jQuery 之前运行的增强.js 库 onScriptLoaded

发布于 2024-10-01 12:36:30 字数 1326 浏览 4 评论 0原文

请使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

白鸥掠海 2024-10-08 12:36:30

我认为这就是您想要的,请注意 < code>onScriptsLoaded 直接用作回调,因此它应该是一个函数,而不是数组:

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'
   ],
   onScriptsLoaded: function() { alert($); }
});

或者更一般地,对于函数,仅传递引用,如下所示:

   onScriptsLoaded: runMyCode

原因 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:

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'
   ],
   onScriptsLoaded: function() { alert($); }
});

Or more generally for a function, pass just the reference like this:

   onScriptsLoaded: runMyCode

The reason onScriptsLoaded:[runMyCode()] "kind of" works is it's immediately invoking the runMyCode 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, since onScriptsLoaded is expected to be a callback function, not an array.

北城挽邺 2024-10-08 12:36:30

我认为这可能是一个问题:

onScriptsLoaded:[runMyCode()]

您尝试过吗(假设数组符号是正确的):(编辑 - 它正确)

onScriptsLoaded: [runMyCode]

?第一种方法是在设置库时调用函数。您只想传递对函数的引用。

编辑实际上它应该是:

onScriptsLoaded: runMyCode

我不知道数组来自哪里; “enhance.js”文档中绝对没有提到它。

I think this may be a problem:

onScriptsLoaded:[runMyCode()]

have you tried (assuming the array notation is correct): (edit — it is not correct)

onScriptsLoaded: [runMyCode]

? 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:

onScriptsLoaded: runMyCode

I don't know where the array came from; it's definitely not mentioned in the "enhance.js" documentation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文