My webpage has one existing window.onload javascript function defined by a javascript plugin in the <head>
section. Now, I defined a new one and add it to the end of the HTML page:
我的网页有一个现有窗口。 由<head>
部分中的javascript 插件定义的onload javascript函数。 现在,我定义了一个新代码,并将其添加到HTML页面的末尾:
<script type="text/javascript">
window.onload = function () {
// great work here
}
</script>
I find the new one overwrites the old one in the plugin and the old one is not executed at all.
我发现新插件会覆盖插件中的旧插件,而旧插件根本不会执行。
How to add my onw window.onload functions without overwriting old ones?
如何添加我的onw window.onload功能而不覆盖旧功能?
I find this little trick by rewriting the javascript as follows:
通过重写javascript,我发现了这个小技巧:
<script type="text/javascript">
var oldOnload = window.onload;
window.onload = function () {
if (typeof oldOnload == 'function') {
oldOnload();
}
// great work here
}
</script>
翻译自: https://www.systutorials.com/add-my-own-window-onload-safely-without-overwriting-old-ones/