sandoche / Darkmode.js

🌓 Add a dark-mode / night-mode to your website in a few seconds
https://darkmodejs.learn.uno
MIT License
2.73k stars 175 forks source link

Using snippet won't work. #37

Closed keanuplayz closed 4 years ago

keanuplayz commented 4 years ago

For my own website, I decided to give this a shot.

I added the snippet that was given to me to my own page:

<script src="https://cdn.jsdelivr.net/npm/darkmode-js@1.5.5/lib/darkmode-js.min.js"></script>
<script>
  new Darkmode().showWidget();
</script>

But to my surprise, nothing happened.

I checked the console, and found this:

darkmode-js.min.js:1 Uncaught TypeError: Cannot read property 'classList' of null
    at new e (darkmode-js.min.js:1)

I haven't done anything weird.

I even created a new html document with nothing in it except a default template with some text, added the snippet, and nothing happened.

Any help?

sandoche commented 4 years ago

It may be because you use it without a localserver it may happen if you open directly the html file.

keanuplayz commented 4 years ago

I am using a local server to preview my files.

sandoche commented 4 years ago

Do you have any extension blockcking things ? please try with another browser maybe.

SaadAhmadSaddiqui commented 4 years ago

Hi, Sandoche. I am facing the same problem. I put the snippet in the head tag of the master page in ASP.Net and it doesn't work. The error is the same as the one above on both Chrome and Edge.

darkmode-js.min.js:1 Uncaught TypeError: Cannot read property 'classList' of null at new e (darkmode-js.min.js:1)

StafDehat commented 4 years ago

I think the reason this is happening, is because the script attempts to append a DOM object before the DOM tree has loaded. You'll need to call showWidget() after the page has loaded - you can do that by putting it inside an event listener for "load". I made a PR to update the readme as such - see here for details: https://github.com/sandoche/Darkmode.js/pull/47/files

keanuplayz commented 4 years ago

That indeed managed to fix my issue a couple of days back. (see here) I believe this can finally be closed now that we have a definite fix.