From 2c8485f32252b29d8c932d807591a2c9e0cf616e Mon Sep 17 00:00:00 2001 From: Nicolas Coden Date: Sat, 30 Jun 2018 23:16:32 +0200 Subject: [PATCH] fix: prevent removing all listeners by checking them before unbinding $(...).off(undefined) removes all the attached event listeners, including those outside Foundation of unrelated to the issue. This commit fixes #11360 and probably others issues by checking for variables expected to be listeners names before using it to remove the listeners. [This fix is compatible with v6.5] Closes https://github.com/zurb/foundation-sites/issues/11360 --- js/foundation.magellan.js | 5 ++--- js/foundation.offcanvas.js | 2 +- js/foundation.reveal.js | 5 ++--- js/foundation.sticky.js | 5 ++--- js/foundation.tabs.js | 4 +++- 5 files changed, 10 insertions(+), 11 deletions(-) diff --git a/js/foundation.magellan.js b/js/foundation.magellan.js index 68784a7f00..990c1ada19 100644 --- a/js/foundation.magellan.js +++ b/js/foundation.magellan.js @@ -227,9 +227,8 @@ class Magellan extends Plugin { window.location.hash.replace(hash, ''); } - $(window) - .off('hashchange', this._deepLinkScroll) - .off(this.onLoadListener); + $(window).off('hashchange', this._deepLinkScroll) + if (this.onLoadListener) $(window).off(this.onLoadListener); } } diff --git a/js/foundation.offcanvas.js b/js/foundation.offcanvas.js index 3d442f9fa1..8558edeef0 100644 --- a/js/foundation.offcanvas.js +++ b/js/foundation.offcanvas.js @@ -480,7 +480,7 @@ class OffCanvas extends Plugin { this.close(); this.$element.off('.zf.trigger .zf.offCanvas'); this.$overlay.off('.zf.offCanvas'); - $(window).off(this.onLoadListener); + if (this.onLoadListener) $(window).off(this.onLoadListener); } } diff --git a/js/foundation.reveal.js b/js/foundation.reveal.js index 27875c2665..b11fba3887 100644 --- a/js/foundation.reveal.js +++ b/js/foundation.reveal.js @@ -502,9 +502,8 @@ class Reveal extends Plugin { } this.$element.hide().off(); this.$anchor.off('.zf'); - $(window) - .off(`.zf.reveal:${this.id}`) - .off(this.onLoadListener); + $(window).off(`.zf.reveal:${this.id}`) + if (this.onLoadListener) $(window).off(this.onLoadListener); if ($('.reveal:visible').length === 0) { this._removeGlobalClasses(); // also remove .is-reveal-open from the html element when there is no opened reveal diff --git a/js/foundation.sticky.js b/js/foundation.sticky.js index 9997db5a04..3ae09866ba 100644 --- a/js/foundation.sticky.js +++ b/js/foundation.sticky.js @@ -404,9 +404,8 @@ class Sticky extends Plugin { if (this.$anchor && this.$anchor.length) { this.$anchor.off('change.zf.sticky'); } - $(window) - .off(this.scrollListener) - .off(this.onLoadListener); + if (this.scrollListener) $(window).off(this.scrollListener) + if (this.onLoadListener) $(window).off(this.onLoadListener) if (this.wasWrapped) { this.$element.unwrap(); diff --git a/js/foundation.tabs.js b/js/foundation.tabs.js index b946070ed9..c8013f367d 100644 --- a/js/foundation.tabs.js +++ b/js/foundation.tabs.js @@ -398,7 +398,9 @@ class Tabs extends Plugin { $(window).off('hashchange', this._checkDeepLink); } - $(window).off(this.onLoadListener); + if (this.onLoadListener) { + $(window).off(this.onLoadListener); + } } }