Add my own window.onload safely without overwriting old ones

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:

<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?

asked May 22, 2014 by Eric Z Ma (44,280 points)

1 Answer

 
Best answer

I find this little trick by rewriting the javascript as follows:

<script type="text/javascript">
    var oldOnload = window.onload;

    window.onload = function () {

        if (typeof oldOnload == 'function') {
          oldOnload();
        }

        // great work here
    }
</script>
answered May 22, 2014 by Eric Z Ma (44,280 points)

Please log in or register to answer this question.

Copyright © SysTutorials. User contributions licensed under cc-wiki with attribution required.
Hosted on Dreamhost

...