wnr / element-resize-detector

Optimized cross-browser resize listener for elements.
MIT License
1.32k stars 118 forks source link

Element is not detectable by this strategy #95

Closed mkrivan closed 5 years ago

mkrivan commented 6 years ago

When I use IE8 an exception raised which comes from object strategy code (detection-strategy/object.js) see below:

function addListener(element, listener) {
    if(!getObject(element)) {
        throw new Error("Element is not detectable by this strategy.");
    }

    function listenerProxy() {
        listener(element);
    }

    if(browserDetector.isIE(8)) {
        //IE 8 does not support object, but supports the resize event directly on elements.
        getState(element).object = {
            proxy: listenerProxy
        };
        element.attachEvent("onresize", listenerProxy);
    } else {
        var object = getObject(element);
        object.contentDocument.defaultView.addEventListener("resize", listenerProxy);
    }
}

I found it quite obvious because the object value is missing in IE8 (mentioned as well in the documentation). But there is also a part in this method where IE8 is checked and the object value is created. When I have commented out the getObject() check everything worked perfect.

Somewhere it is mentioned that the scroll strategy could also work but in IE8 unfortunately not. A quite strange runtime exception occures.

Would you check my reflection? Many thanks for all of your efforts. This library is something extremely useful.

wnr commented 6 years ago

Hi @mkrivan and thank you for your thoughts. Indeed, this seems like a mistake. That IE8 check should probably be moved from the object strategy somewhere to https://github.com/wnr/element-resize-detector/blob/master/src/element-resize-detector.js#L117. It seems like the main code should check if IE8 and then choose this "native" strategy regardless desired strategy.

Are you interested in creating a pull request? Otherwise it will probably take me a few days to fix this myself.

wnr commented 6 years ago

I believe this has now been fixed in the https://github.com/wnr/element-resize-detector/tree/WIP-1.2.0 branch, could you give it a try?