admin管理员组文章数量:1355697
In a previous question I learned of a bug in my JavaScript related to using appendChild on the BODY element before the DOMReady event was fired.
I was able to resolve this by wrapping my code in a window.onload event. Reading up on this, I have learned that window.onload only supports a single listener, and I can't guarantee I am not clobbering someone else's listener or that someone else's listener is not clobbering mine.
Looking at the source for $(document).ready(function(){});
, I realize there is perhaps a lot that goes into the simplicity of that syntax.
Is there a universal way for me to wait for the DOMReady event without using jQuery or another JS library? While it seems like that would do the trick, I would prefer not to have any dependencies on a third party library in order for my code to work correctly.
In a previous question I learned of a bug in my JavaScript related to using appendChild on the BODY element before the DOMReady event was fired.
I was able to resolve this by wrapping my code in a window.onload event. Reading up on this, I have learned that window.onload only supports a single listener, and I can't guarantee I am not clobbering someone else's listener or that someone else's listener is not clobbering mine.
Looking at the source for $(document).ready(function(){});
, I realize there is perhaps a lot that goes into the simplicity of that syntax.
Is there a universal way for me to wait for the DOMReady event without using jQuery or another JS library? While it seems like that would do the trick, I would prefer not to have any dependencies on a third party library in order for my code to work correctly.
Share Improve this question edited May 23, 2017 at 10:27 CommunityBot 11 silver badge asked Jun 8, 2011 at 21:25 buleybuley 29.3k18 gold badges88 silver badges108 bronze badges2 Answers
Reset to default 6You can do something like:
function addLoadEvent(func) {
var oldonload = window.onload;
if (typeof window.onload != 'function') {
window.onload = func;
} else {
window.onload = function() {
if (oldonload) {
oldonload();
}
func();
}
}
}
addLoadEvent(nameOfSomeFunctionToRunOnPageLoad);
addLoadEvent(function() {
/* more code to run on page load */
});
which wraps the existing onload
in a new function with an additional function. This is taken from http://www.webreference./programming/javascript/onloads/
However, for doing it on document ready and not onload (different things, onload is when everything is downloaded where ready is when the document are loaded) look at this question: $(document).ready equivalent without jQuery
You can use:
document.onpageshow= new function() {
//(Code here)
}
You can also replace "onpageshow" with "onload" for better patibility.
This may also be helpful: https://developer.mozilla/en/DOM/element.addEventListener
本文标签: javascriptAre there universal alternatives to windowonload without using frameworksStack Overflow
版权声明:本文标题:javascript - Are there universal alternatives to window.onload without using frameworks? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743974548a2570823.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论