Kosmetika
Kosmetika

Reputation: 21304

apply() function Internet explorer

Did anybody cope with this issue?

Code after apply doesn't execute in Internet Explorer

originalFocus.apply(this, arguments);
$(document).scrollTop(300);

scrollTop doesn't execute at all in IE!

but if put alert() for example it will work

originalFocus.apply(this, arguments);
alert('ha');
$(document).scrollTop(300);

here is the full code:

(function($) {
    var originalFocus = $.fn.focus;
    $.fn.focus = function () {
        if (this.hasClass('no-scroll')) {
            var y = (window.pageYOffset !== undefined) ? window.pageYOffset : (document.documentElement || document.body).scrollTop;

            originalFocus.apply(this, arguments);
            $(document).scrollTop(y);
        } else {
            return originalFocus.apply(this, arguments);
        }
    };
})(jQuery);

ANSWER

Well, guys, I found a problem - needed to return a function, simple)

(function($) {
var originalFocus = $.fn.focus;
$.fn.focus = function () {

    return function() {
        if (this.hasClass('no-scroll')) {
            var y = (window.pageYOffset !== undefined) ? window.pageYOffset : (document.documentElement || document.body).scrollTop;

            originalFocus.apply(this, arguments);
            window.scrollTo(x, y);
        } else {
            originalFocus.apply(this, arguments);
        }
    };

};
})(jQuery);

Upvotes: 1

Views: 935

Answers (1)

Pointy
Pointy

Reputation: 413767

Sometimes, trying to force focus inside an event handler can cause problems. Try doing it in a timer handler:

      var this2 = this;
      setTimeout(function() {
        originalFocus.apply(this2, arguments);
      }, 1);

Upvotes: 1

Related Questions