biati-digital / glightbox

Pure Javascript lightbox with mobile support. It can handle images, videos with autoplay, inline content and iframes
MIT License
2.08k stars 229 forks source link

error at line 523 - function isMobile(): "window" is not defined #490

Closed streimi closed 3 months ago

streimi commented 6 months ago

So whenever I compile/run my app, mentioned error pops up, saying "window is not defined", pointing to the function "isMobile" in line 523 of glightbox.js. However, before the function there are some if queries, where window is also used multiple times, seemingly without any problems, so my question now is, what's the deal?

I'm using glightbox via <script src="./components/js/vendor/glightbox/js/glightbox.min.js"></script> in the main Header. (/app/Header.js)

My file structure is the following: app/page_&_header_js app/components/jsx_files app/a_sub_page

The culprit:

function isMobile() {
    return (
      "navigator" in window &&
      window.navigator.userAgent.match(
        /(iPad)|(iPhone)|(iPod)|(Android)|(PlayBook)|(BB10)|(BlackBerry)|(Opera Mini)|(IEMobile)|(webOS)|(MeeGo)/i
      )
    );
  }
gingerchew commented 5 months ago

That sounds like whatever framework you're using is trying to run the code on the server, you should see if there is a way to isolate the code to run client only per your framework.