glide throws an error when root element is not present

Created on 17 Aug 2018  路  4Comments  路  Source: glidejs/glide

To replicate:

  1. Initialize glide with a name of a class that does not exist on the page.
  2. Look at the console output

The error is related to this query in glide sourcecode:
this.track = this.root.querySelector(TRACK_SELECTOR);
this.root doesn't exist so it throws error glide.js:1308 Uncaught TypeError: Cannot read property 'querySelector' of undefined

It would be good if glide had guard condition that prevents errors when it can't find element with a provided class.

Most helpful comment

You have to verify if HTMLElement exists before initializing glide instance. The component base structure makes it hard to bail that as there is no single point where this could be tackled down.

var slider = document.querySelector('.glide');

if (slider) {
  var glide = new Glide(slider);

  glide.mount();
}

Also more explanation here #283

All 4 comments

I also noticed this. Surely the script should just bail if there is no matching element as early as possible.

Yep - in my particular use case where I don't have the Glide component on a page at all times, this is certainly annoying.

You have to verify if HTMLElement exists before initializing glide instance. The component base structure makes it hard to bail that as there is no single point where this could be tackled down.

var slider = document.querySelector('.glide');

if (slider) {
  var glide = new Glide(slider);

  glide.mount();
}

Also more explanation here #283

This is a very weird behavior. This is really the only thing that bothers me with this awesome library.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

philmarc picture philmarc  路  3Comments

thany picture thany  路  3Comments

dodozhang21 picture dodozhang21  路  5Comments

Danielvandervelden picture Danielvandervelden  路  4Comments

sehsarah picture sehsarah  路  5Comments