Support

Account

Home Forums Gutenberg Enqueueing a script when a block is used

Solved

Enqueueing a script when a block is used

  • I’ve created a gallery block that needs JS to work, but I only use it on a few pages and don’t want to enqueue a heavy JS file on my whole website. Is there a way to check if a block is used on a page and enqueue a script only when its needed?

  • There isn’t any reason to check, really. When you display the block content just enqueue the script there in the footer. You can event “enqueue” a script multiple times and only the first one will count. As an example, I am currently working on a site where I’m using slick slider. I have set up multiple flexible content layouts that use it. Inside my code that displays the flex layout I enqueue the scripts I need.

  • I’ll do that, thanks for the information 🙂

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.