javascriptangularjsscrollcross-browseriscroll

window.dispatchEvent doesn't work in Firefox, Safari or IE


I need to force a resize of my browser window due to a promise that breaks IScroll on a very wide element (the problem is that I use a promise to change the width if promise succeeds). IScroll loads before the promise is finished and gets the with from the original element. The result is that I can only scroll as wide as the original element width, not the new width of the wider element loaded by the promise. If I manually resize the browser window, I can then scroll the full size.

I fixed this by changing IScroll's maxScrollX and scrollerWidth after the promise has loaded, but I also use an indicator that doesn't scroll all the way.

I try to fix this using window.dispatchEvent(new Event('resize')); to force a resize of the window. My problem is that this works fine in Chrome, but breaks the scroll altogether in Firefox, Safari and IE. Is there some other way to force a resize of the window that is compatible with all browsers?

I tried the following browsers which doesn't work:


Solution

  • I finally found the answer myself:

    $timeout(function() {
        var evt = $window.document.createEvent('UIEvents'); 
        evt.initUIEvent('resize', true, false, $window, 0); 
        $window.dispatchEvent(evt);
    });