Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Darkmode is not defined #77

Open
heyepic opened this issue Sep 11, 2021 · 1 comment
Open

Darkmode is not defined #77

heyepic opened this issue Sep 11, 2021 · 1 comment

Comments

@heyepic
Copy link

heyepic commented Sep 11, 2021

First off - thanks for making this and I hope I can get this working!

I'm implementing this on a Magento 2 store and I'm including it near the footer. However, I'm seeing the following error in console log:
(index):4595 Uncaught ReferenceError: Darkmode is not defined at addDarkmodeWidget ((index):4595)

I'm using the simple js include method here:

<html>
<body>
<!--StartFragment-->

<script src="https://cdn.jsdelivr.net/npm/darkmode-js@1.5.7/lib/darkmode-js.min.js"></script>
--
  | <script>
  | function addDarkmodeWidget() {
  | new Darkmode().showWidget();
  | }
  | window.addEventListener('load', addDarkmodeWidget);
  | </script>

<!--EndFragment-->
</body>
</html>

Any ideas?

@ivanaugustobd
Copy link

ivanaugustobd commented Jun 6, 2023

Take a look at the top of the darkmode js file. You'll notice this:
image

So the trick is to use the defined name there in a requirejs() call (not require(), not define(), literally requirejs()):

<script src="https://cdn.jsdelivr.net/npm/darkmode-js@1.5.7/lib/darkmode-js.min.js"></script>
<script>
  window.addEventListener('load', () => {
    requirejs(['darkmode-js'], Darkmode => new Darkmode().showWidget())
  })
</script>

Reference: https://stackoverflow.com/a/14033636/2478283

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants