Search code examples
javascriptphpjqueryajaxreload

Reload script (Poptrox) after ajax request


So I'm using an AJAX request to load data from the database into a DIV. At the same time, I'm using the same data inside the DIV in a different script (Poptrox) witch does some design stuff.

Without the AJAX request, poptrox works, but if I'm using it together with poptrox it doesn't do the designing anymore.

Is there a way to reload the poptrox-script after the ajax request?

NOTE: the AJAX-Code and the Poptrox-code are placed inside the "main.js"!

Thanks in advance!

php:

<script src="assets/js/jquery.min.js"></script>
<section class="wrapper">
    <form action="kochbuch.html" method="get" id="searchfilter">
        <input type="text" class="searchBox" name="searchBox" id="searchBox" placeholder="Search..">
        <button type="submit" id="searchBtn"><i class="fas fa-search"></i></button>
    </form>
</section>
<section class="wrapper">
<div id="gerichte"></div>
</section>
<script src="assets/js/main.js"></script>

js-ajax:

var queryString = window.location.search;
var urlParams = new URLSearchParams(queryString);
if (urlParams.has('searchBox')){
                    var searchBox = urlParams.get('searchBox');
                    $.ajax({
                                    type: "POST",
                                    url: "kochbuch/fetchdata.php",
                                    data: {
                                                    "search_post_btn": 1,
                                                    "searchBox": searchBox,
                                    },
                                    dataType: "json",
                                    success: function (response) {
                                                    $("#gerichte").html(response);
                                    }
                 });
            };

js-poptrox:

// Gerichte.
        var $gerichte = $('#gerichte');

        // Thumbs.
            $gerichte.children('.thumb').each(function() {

                var $this = $(this),
                    $image = $this.find('.image'), $image_img = $image.children('img'),
                    x;

                // No image? Bail.
                    if ($image.length == 0)
                        return;

                // Image.
                // This sets the background of the "image" <span> to the image pointed to by its child
                // <img> (which is then hidden). Gives us way more flexibility.

                    // Set background.
                        $image.css('background-image', 'url(' + $image_img.attr('src') + ')');

                    // Set background position.
                        if (x = $image_img.data('position'))
                            $image.css('background-position', x);

                    // Hide original img.
                        $image_img.hide();

            });


    // Poptrox.
        $gerichte.poptrox({
            baseZIndex: 20000,
            caption: function($a) {

                var s = '';

                $a.nextAll().each(function() {
                    s += this.outerHTML;
                });

                return s;

            },
            fadeSpeed: 300,
            onPopupClose: function() { $body.removeClass('modal-active'); },
            onPopupOpen: function() { $body.addClass('modal-active'); },
            overlayOpacity: 0,
            popupCloserText: '',
            popupHeight: 150,
            popupLoaderText: '',
            popupSpeed: 300,
            popupWidth: 150,
            selector: '.thumb > a.image',
            usePopupCaption: true,
            usePopupCloser: true,
            usePopupDefaultStyling: false,
            usePopupForceClose: true,
            usePopupLoader: true,
            usePopupNav: true,
            windowMargin: 50
        });

        // Hack: Set margins to 0 when 'xsmall' activates.
            breakpoints.on('<=xsmall', function() {
                $main[0]._poptrox.windowMargin = 0;
            });

            breakpoints.on('>xsmall', function() {
                $main[0]._poptrox.windowMargin = 50;
            });

Solution

  • alright, i found one possible solution, but know the preloaded data ist formated:

    the whole poptrox code has to be wrapped by this function:

    $(document).ajaxStop(function () {
    
         // function after ajax
    
    });
    

    If you know any better solution, feel free to submit :)

    EDIT:

    alright, my solution was as follows:

    if (urlParams.has('searchBox')){
            var searchBox = urlParams.get('searchBox');
    } else {
        var searchBox = '';
    }
    
        $.ajax({ .....