Jquery 初始化混淆
Jquery initialization confusion
现在,无论出于何种原因,原作者在初始化时做了一些我不太理解的事情。有这段代码在我看来是多余的:
if (document.addEventListener) {
document.addEventListener('DOMContentLoaded', init, false);
}
(function() {
/*@cc_on
try {
document.body.doScroll('up');
return init();
} catch(e) {}
/*@if (false) @*/
if (/loaded|complete/.test(document.readyState)) return init();
/*@end @*/
if (!init.done) setTimeout(arguments.callee, 30);
})();
if (window.addEventListener) {
window.addEventListener('load', init, false);
} else if (window.attachEvent) {
window.attachEvent('onload', init);
}
function init()
{
if (arguments.callee.done) return;
arguments.callee.done = true;
// do your thing
//[...]
}
这样做的目的是什么?还是胡说八道?
代码确保调用init()
函数。
它将 init 函数绑定到加载 DOM 或页面时触发的事件侦听器。
如果这些事件已经由 readyState 确定触发,那么它直接调用 init,否则它会每 30 毫秒检查一次 readyState。
// Call init function when DOM is loaded
if (document.addEventListener) {
document.addEventListener('DOMContentLoaded', init, false);
}
// Immediately invoked function expression that calls init
// function if doScroll method does not throw error.
(function() {
try {
document.body.doScroll('up');
return init();
} catch(e) {}
// Call init function if DOMContentLoaded event has already been
// fired or if page is already loaded.
if (/loaded|complete/.test(document.readyState)) return init();
// arguments.callee is a reference to it's executing function
// which is this immediately invoked function expression.
// It will keep calling it every 30 milliseconds while init
// has not been called yet.
if (!init.done) setTimeout(arguments.callee, 30);
})();
// Call init function when window is loaded.
// `load` event is fired after DOMContentReady, when
// everything has loaded in the page.
if (window.addEventListener) {
window.addEventListener('load', init, false);
// Same as above but for IE versions 8 or less
} else if (window.attachEvent) {
window.attachEvent('onload', init);
}
function init() {
// If init has been called then immediately return.
if (arguments.callee.done) return;
// Set flag on itself to indicate that it init been called.
arguments.callee.done = true;
// do your thing
//[...]
}
相关文章:
- 在XMLHttpRequest之后重新初始化jQuery
- 通过删除其相关的 DOM 存在来重新初始化 jQuery 插件
- 如何在初始化jQuery.ajax成功函数后覆盖它
- 初始化 jQuery 数据表中的搜索输入
- 在多个动态添加的表单上初始化 jQuery validate() 函数
- 无法在按钮单击Ajax调用时初始化Jquery数据表
- ReactJS - 在 react 类中初始化 jquery 插件的标准方法是什么
- 如何在 AJAX 加载的选项卡中初始化 jquery 函数
- 在 AJAX 页面更新后重新初始化 jQuery 插件
- 是否可以在 ajax 调用后重新初始化 jQuery 的所有插件
- 防止重新定位导航初始化 jquery mmenu
- 如何重新初始化jQuery文件上传数据数组
- 从数据属性初始化JQuery插件
- 用于脚本加载的预初始化jQuery代码不起作用
- 使用数组的本地数组作为数据源初始化Jquery DataTable
- 如何在Handlebars模板中初始化jQuery小部件
- 初始化Jquery动画函数的值
- 在ajax之后重新初始化jquery中的变量
- 初始化jQuery返回空白页的数据表
- 加载附加信息后重新初始化jQuery