ten1seven / what-input

A global utility for tracking the current input method (mouse/pointer, keyboard or touch).
https://ten1seven.github.io/what-input
MIT License
1.35k stars 89 forks source link

Uncaught TypeError: Cannot read property 'dataset' of null #101

Closed sn3p closed 4 years ago

sn3p commented 4 years ago

Got an error bumping what-input from 5.2.3 to 5.2.4:

Uncaught TypeError: Cannot read property 'dataset' of null

At line:

https://github.com/ten1seven/what-input/blob/ec9a7d23ed6aa6b3c66d42f603069e4dd8131749/dist/what-input.js#L118

(breaks on document.body.dataset.whatpersist to be precise)

Not sure why body is null here, but maybe be because we're using Turbolinks in our app?

ten1seven commented 4 years ago

Hi @sn3p,

Yikes! Thanks for reporting this. I just released this feature and must have missed the error in testing. I'm going to work on a fix this morning.

ten1seven commented 4 years ago

I just pushed up a fix as v5.2.5. Can you give it a try and let me know if that fixes the issue? If not, please let me know what browser version you're seeing the issue in.

sn3p commented 4 years ago

@ten1seven thanks for the fast response, but sorry that doesn't fix my issue 😢 The problem is that document.body is undefined, so calling getAttribute on body results in the same error:

Uncaught TypeError: Cannot read property 'getAttribute' of null

The weird thing is that document.body is null here 🤔

If not, please let me know what browser version you're seeing the issue in.

I'm using Chrome 77.0.3865.90 on macOS (latest at the time of writing).

sn3p commented 4 years ago

Looks like document.body isn't available yet at the time of execution. In other words the DOM is not ready (similar issue here).

My app.js is included in the head of the document. Here is a part of the source to illustrate the problem:

// import "what-input";

console.log(document.body); // null

document.addEventListener("DOMContentLoaded", () => {
  console.log(document.body); // <body>...</body>
});
ten1seven commented 4 years ago

Yes, of course! I should know better. Thanks for investigating that. I’ll have that update for you shortly.

ten1seven commented 4 years ago

Version 5.2.6 is now up for you to try out. I tested more thoroughly by moving the script tag into the <head>. Should work now.

sn3p commented 4 years ago

Yes!! Works perfectly many thanks for the quick fix ⚡️

akhilesh888 commented 3 years ago

Hi In my case , it was fixed only by rebuilding and serve. Thank You

xcelance commented 3 years ago

I think this is jquery version error please update jquery version

ten1seven commented 3 years ago

@xcelance jQuery isn't used in what-input