Search code examples
javascriptjqueryinternet-explorerdom-events

.ready() triggered after window.beforeunload


We have a few places where we include inline <script> blocks which run code, wrapped by $(document).ready(). Several of these blocks use methods dependent on other external scripts. During normal page execution this technique works fine, but when the user navigates away from the page before it's completely loaded, the $(document).ready() event is triggered and IE is throwing these errors:

"Object does not support this property or method"

Through some testing I've found out that the window.beforeunload event is triggered before any of the ready events, so I'd like to be able to prevent the ready events from being triggered at that point. Ideally I'd like to have a solution that generalizes so I don't have to modify all of the inline code.

If that's not possible would you suggest wrapping all of the inline code in try-catch blocks, determining if all the external scripts have been loaded before executing the inline code, etc?


Solution

  • Ended up going with the following solution:

    <script type="text/javascript">
     window.onbeforeunload = function(){
      $.readyList = null;
     }
    </script>
    

    It's a little hackier than I would have preferred, but it gets the job done.