Skip to content

Commit

Permalink
Allows graceful fail when element not found on page - warns and remov…
Browse files Browse the repository at this point in the history
…es from scrollItems
  • Loading branch information
kirbee-parsons-hs committed Nov 30, 2016
1 parent 950cd19 commit 432a75f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
12 changes: 8 additions & 4 deletions dist/menuspy.js
@@ -1,4 +1,4 @@
/*! MenuSpy v1.0.0 (Aug 29 2016) - http://lcdsantos.github.io/menuspy/ - Copyright (c) 2016 Leonardo Santos; MIT License */
/*! MenuSpy v1.0.0 (Nov 29 2016) - http://leocs.me/menuspy/ - Copyright (c) 2016 Leonardo Santos; MIT License */
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
Expand Down Expand Up @@ -112,10 +112,14 @@ MenuSpy.prototype.assignValues = function assignValues () {
MenuSpy.prototype.cacheItems = function cacheItems () {
this.scrollItems = this.menuItems.map(function (a) {
var elm = document.querySelector(a.getAttribute('href'));
var offset = utils.offset(elm).top;

return { elm: elm, offset: offset };
if (elm) {
var offset = utils.offset(elm).top;
return { elm: elm, offset: offset };
} else {
console.warn('MenuSpy warning: %s not found on page.', a.href);
}
});
this.scrollItems = this.scrollItems.filter( Boolean );
};

MenuSpy.prototype.tick = function tick () {
Expand Down
4 changes: 2 additions & 2 deletions dist/menuspy.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 7 additions & 3 deletions src/menuspy.js
Expand Up @@ -44,10 +44,14 @@ class MenuSpy {
cacheItems() {
this.scrollItems = this.menuItems.map((a) => {
const elm = document.querySelector(a.getAttribute('href'));
const offset = utils.offset(elm).top;

return { elm, offset };
if (elm) {
const offset = utils.offset(elm).top;
return { elm, offset };
} else {
console.warn('MenuSpy warning: %s not found on page.', a.href);
}
});
this.scrollItems = this.scrollItems.filter( Boolean );
}

tick() {
Expand Down

0 comments on commit 432a75f

Please sign in to comment.