YepNopeJS:在小型JS文件完全加载之前触发回调 [英] YepNopeJS: callback triggered before tiny JS file is fully loaded

查看:70
本文介绍了YepNopeJS:在小型JS文件完全加载之前触发回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用Modernizr.load(又名YepNopeJS)加载各种外部脚本的站点。其中一个脚本是一个小型统计脚本(3,5 kB未压缩),具有非常简单的回调函数:

I've got a site which is loading all kinds of external scripts using Modernizr.load (aka YepNopeJS). One of those scripts is a small statistics script (3,5 kB uncompressed) with a very simple callback function:

Modernizr.load({
  load: 'http://res.xtractor.no/x.js',
  callback: function() { _pxReg(); }
});

_pxReg只是x.js脚本中定义的函数。在大约50%的页面中,我收到错误消息Undefined variable:_pxReg(Opera 12)或'_pxReg'未定义(Internet Explorer 9)。 Firefox 15和Chrome 22从不出错。重新加载页面时不会发生错误。

_pxReg is simply a function defined in the x.js script. In about 50% of my pages, I receive an error message "Undefined variable: _pxReg" (Opera 12) or "'_pxReg' is undefined" (Internet Explorer 9). Firefox 15 and Chrome 22 never give errors. The error never occurs when reloading a page.

这似乎是某种时间错误。在IE开发人员工具中,x.js文件的源在发生错误时为空。这似乎与YepNope中的双重请求bug有关( http://yepnopejs.com/#twice),因为在IE中你得到两个对每个加载的JS文件的引用,其中第一个总是空的。

This seems to be a timing error of some sort. In the IE developer tools, the source of the x.js file is empty at the moment the error occurs. This seems to be related to the double-request "bug" in YepNope (http://yepnopejs.com/#twice), because in IE you get two references to every loaded JS file where the first one is always empty.

我本来希望产生一个触发的完整示例这个错误,但似乎与页面的复杂性有关。页面的简化版本永远不会触发此错误。

I would have liked to produce a complete example that triggers this error, but it seems to related to the complexity of the page. Simplified versions of the page never trigger this error.

是否有人知道可能导致此问题的原因,以及我应该如何修复它?网站地址为 http://www.husbanken.no/

Does anybody know anything about what could be causing this, and how I should fix it? The site address is http://www.husbanken.no/.

推荐答案

每次资源加载都会发生一次回调,
意味着它会在加载n时执行一次:['x.js','y.js' ]

The callback happens once per resource load, meaning it will execute once for n in load : ['x.js', 'y.js']

如果您使用yepnope.js加载资源(基本上Modernizr.load具有更多
能力:前缀,超时等)
并提供一个回调,你可以捕获
闭包中的键并执行你的函数:

If you load the resource using yepnope.js(basically Modernizr.load with more abilities: prefixes, timeouts and more) and provide a callback, you can catch the keys in the closure and execute your function like this:

yepnope([{
    load : {'x':'//x.js'},
    callback : function(url, result, key){
        if("x" === key && result === true) { _pxReg() };
    },
    complete : function(){ console.log("all done..")}
}])

如果有帮助请告诉我:))

Let me know if this helps :)

这篇关于YepNopeJS:在小型JS文件完全加载之前触发回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆