Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use PhotoSwipe for old versions of JavaScript by including it from a CDN?

Tags:

javascript

PhotoSwipe only shows the code for ESM modules.

<script type="module">
import PhotoSwipeLightbox from 'photoswipe/dist/photoswipe-lightbox.esm.js';
const lightbox = new PhotoSwipeLightbox({
  gallery: '#my-gallery',
  children: 'a',
  pswpModule: () => import('photoswipe/dist/photoswipe.esm.js')
});
lightbox.init();
</script>

This does not work with my current setup. I'm working in Visual Studio on a project that uses jQuery and old ways of including JS files in HTML via <script> tags and CDNs.

If I use <script src='/path-to/photoswipe.esm.js'> it apparently won't work because this file contains an export keyword in it and it shows an error inside console:

Uncaught SyntaxError: Unexpected token 'export'

So, is there a way for me to use this library, but with old-school code?

like image 966
Ali EXE Avatar asked Nov 29 '25 13:11

Ali EXE


1 Answers

v5 version offer umd build, you are able to use that build to run the old way

<!DOCTYPE html>
<html>
  <head>
    <title>Test Gallery</title>
  </head>
  <body>

    <script src="./photoswipe.umd.min.js"></script>
    <script src="./photoswipe-lightbox.umd.min.js"></script>
    
    <link rel="stylesheet" href="../photoswipe.css">

    <div class="test-gallery">
      <a href="https://dummyimage.com/1200x600/000/fff" data-pswp-width="1200" data-pswp-height="600">
        <img src="https://dummyimage.com/120x60/000/fff" alt="" />
      </a>
      <a href="https://dummyimage.com/1200x1200/000/fff" data-pswp-width="1200" data-pswp-height="1200">
        <img src="https://dummyimage.com/60x60/000/fff" alt="" />
      </a>
      <a href="https://dummyimage.com/600x1200/000/fff" data-pswp-width="600" data-pswp-height="1200">
        <img src="https://dummyimage.com/30x60/000/fff" alt="" />
      </a>
    </div>
    
    <script type="text/javascript">
      var lightbox = new PhotoSwipeLightbox({
        gallery: '.test-gallery',
        children: 'a',
        // dynamic import is not supported in UMD version
        pswpModule: PhotoSwipe 
      });
      lightbox.init();
    </script>

  </body>
</html>

like image 168
Dan Nguyen Avatar answered Dec 02 '25 04:12

Dan Nguyen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!