Custom javascript in Page Builder

Jim Piller asked on November 24, 2020 22:57

I am building a site in Kentico 13 and I'm going to be building ALOT of widgets. Currently my home page has two slick.js carousels on it. when I'm in the page builder interface, the slick scripts don't register and thus the carousels look like stacked images. The MVC site looks fine, but I'm wondering how I get the slick carousel to show up in the Page Builder interface?

Correct Answer

Arjan van Hugten answered on November 27, 2020 14:13

Sorry for answering late. The widgets in the pagebuilder are loaded in the background, so javascript that is added to the bottom of the layout may not be able to find the html elements inside the widgets. You can insert a conditional script in the pagebuilder to initialize your javascript carousel.

You can do something like this in your widget view:

@{ 
    // EnsureStartsWithLetter is our custom HTML ID generator method.
    var carouselGuid = Guid.NewGuid().EnsureStartsWithLetter();
}

<div id="@carouselGuid">
    <!-- Your html -->
</div>

@{
    if (Context.Kentico().PageBuilder().EditMode)
    {
        <script>
            var carousel = document.getElementById('@carouselGuid');
            var intialisedCarousel = new Swiper(carousel, {
                loop: false,
            });
        </script>
    }
}
0 votesVote for this answer Unmark Correct answer

Recent Answers


Jim Piller answered on November 28, 2020 05:51 (last edited on November 28, 2020 06:02)

Thanks for the answer. This seems to have resolved my issue!

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.