js--模拟实现jq中的document.ready
根据jq document.ready的实现原理,贴上js模拟实现该功能的代码:
(function ()
{ var ie = !!(window.attachEvent && !window.opera); //判断ie
var wk = /webkit//(/d+)/i.test(navigator.userAgent) && (RegExp.$1 < 525); //判断webkit内核
var fn = [];
var run = function ()
{
for (var i = 0; i < fn.length; i++) fn[i]();
};
var d = document;
d.ready = function (f)
{
if (!ie && !wk && d.addEventListener) // addEventListener mozilla
return d.addEventListener('DOMContentLoaded', f, false);
if (fn.push(f) > 1) return;
if (ie) (function ()
{
try {
d.documentElement.doScroll('left');
run();
}
catch (err)
{
setTimeout(arguments.callee, 0);
}
}
)();
else if (wk)
var t = setInterval(function ()
{
if (/^(loaded|complete)$/.test(d.readyState))
clearInterval(t), run();
}, 0);
};
})();
//usage
document.ready(function(){check_radio();});