简体   繁体   English

window.onload与jQuery(document).ready

[英]window.onload vs jQuery(document).ready

I was wondering if there are any repercussions I can expect when changing from 我想知道从更改时是否会有任何影响

$(document).ready(function() {...})

to

window.onload = function() {...}

The reason being I am making a widget and do not want to enforce a jQuery include in case the user already has it included in their app, nor do I want them to have to modify the widget code -- so I am dynamically determining if I should include it. 原因是我正在制作窗口小部件,并且不想在用户已将其包含在其应用程序中的情况下强制执行jQuery include,也不想让他们不得不修改窗口小部件代码-因此,我正在动态确定我是否应该包括它。

However, in order to dynamically include it, I do not have access to jQuery before the window.onload , which brings me to my scepticism. 但是,为了动态地包含它,在window.onload之前我无法访问jQuery,这使我感到怀疑。

My main worry is that this will disrupt the functionality of the user's app. 我主要担心的是,这将破坏用户应用程序的功能。 So... will it? 所以...会吗?

Thanks in advance. 提前致谢。

Your function will actually fire in a different point in the page lifecycle. 实际上,您的函数将在页面生命周期的另一点触发。 onload is called early in the lifecycle before all the page elements are necesarily loaded, whereas the ready event fires later. 在必须加载所有页面元素之前,会在生命周期的早期onload ,而稍后会触发ready事件。 If you want to attach to the event without using jQuery, you can easily do that too: 如果您想在不使用jQuery的情况下附加到事件,则也可以轻松地做到这一点:

document.addEventListener('DOMContentReady', function()
{
     // Stuff
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM