miguel-perez / smoothState.js

Unobtrusive page transitions with jQuery.
MIT License
4.43k stars 508 forks source link

Internet Explorer OnClick events not triggering. #341

Open JeremyWildsmith opened 7 years ago

JeremyWildsmith commented 7 years ago

Hello,

I have run into an issue where onclick events are not triggering on pages which are loaded and displayed through smoothstate on Internet Explorer Version 11.0.9600.18698. I believe this has to do with this issue with IE here: https://stackoverflow.com/questions/95731/why-does-an-onclick-property-set-with-setattribute-fail-to-work-in-ie

I have implemented a temporary work-around in the onReady render function which re-registers the event handlers through jquery after the contents have been added to the page.:

onReady: {
            duration: 500,
            // `$container` is a `jQuery Object` of the the current smoothState container
            // `$newContent` is a `jQuery Object` of the HTML that should replace the existing container's HTML.
            render: function ($container, $newContent) {

                var fixOnClick = function ($object) {
                    var onClickAttribute = $object.attr('onclick');

                    if (typeof onClickAttribute !== typeof undefined && onClickAttribute !== false) {
                        $object.removeAttr('onclick');
                        $object.click(function() {return eval(onClickAttribute)});

                    }

                    $object.children().each(function(index, val) {
                        fixOnClick($(val));
                    });
                };

                $container.html($newContent);
                fixOnClick($container);

                 ...