0

I am working on a script to load my page without refreshing. It is working as I want it except for one thing. The test site I have up is http://dev.dog-company.com . Only home About us and FAQ will work with the script right now.

The issue is on the homepage there is a slider loaded via a plugin(click on home while on home). When I load it via AJAX my script does not load it the proper way it is supposed to. I am not sure why. Could someone explain why it is not working?

$('document').ready(function() {
$('a[rel="load"]').click(function(e){
    e.preventDefault();
    var link = $(this).attr("href");
    if(link!=window.location){
                window.history.pushState({path:link},'',link);
    }
    $('#slider-wrapper').slideUp().empty();
    $('#content').wrap('<div id="wrap-overlay"></div>');
    $.ajax({
    //ajax setting
        type: 'POST',
        url: link,
        dataType: 'html',
        success : function(data, text, xhr){
            //parse data
            var response = $("<div>").html(data);

            var parser = new DOMParser();
            var preParse = data.match(/<head[^>]*>[\s\S]*<\/head>/gi);
            var headHTML = parser.parseFromString(preParse, "text/html");
            var head = $(headHTML).find('head').html();
            var slider = response.find('#slider-wrapper').html();
            var content = response.find('#content').html();
            var footer = response.find('#wp-footer').html();

            //Post data
            $('#content').empty().append(content);
            $('head').empty().append(head);
            if(slider != null){
                if($("#slider-wrapper").length == 0){
                    $('#content').insertBefore('<section id="slider-wrapper"></section>');
                }
                $("#slider-wrapper").empty().append(slider);
            }
            $("#content").empty().append(content);
            return false;
        }})
        if($('#slider-wrapper').length > 0){
            $('#slider-wrapper').slideDown();
        }
    });
});

Any help would be appreciated,

4
  • You may need to re-initialize the nivo slider after inserting the slider content inside the success response.( seems you are using nivo slider, correct ? ) Dec 11, 2013 at 8:17
  • Yes I am using nivoslider. Dec 11, 2013 at 8:55
  • This looks more like a javascript problem than a WordPress problem, especially if the issue is a javascript slider inside html returned from an AJAX call. Sure it's on a WordPress site, but you'd be better talking to a Javascript developer than a WordPress developer
    – Tom J Nowell
    Oct 26, 2015 at 18:46
  • Well this issue was 2 years ago and it wasn't the java script that was the problem I think it was that I needed a function that worked with WordPress to get the content I needed. Again this is a 2 year old issue I don't remember much of it. Oct 26, 2015 at 19:46

1 Answer 1

1

You can re-initialize all the scripts once again inside the AJAX success.

// Fetch the scripts
scripts = '';
if ( scripts.length ) scripts.detach();
scripts = response.find('script');

// Add the scripts
scripts.each(function(){
        var script = jQuery(this), 
            scriptText = script.html(), 
            scriptNode = document.createElement('script');
        try {
                // doesn't work on ie...
                scriptNode.appendChild(document.createTextNode(scriptText));
                contentNode.appendChild(scriptNode);
        } catch(e) {
                // IE has funky script nodes
                scriptNode.text = scriptText;
                contentNode.appendChild(scriptNode);
        }
        if(jQuery(this).attr('src') != null) {
                scriptNode.setAttribute('src', (jQuery(this).attr('src')));
        }
});
5
  • Do I reinitialize every script in the document? Dec 11, 2013 at 8:42
  • The script also seems to be not working. I am not sure if you want me to initialize $scripts or not. Dec 11, 2013 at 9:01
  • Updated the answer. Can you check with this ? You could have replaced the $ with jQuery earlier. Dec 11, 2013 at 9:15
  • Updated it. You can try it on the website. I get a error in the console on line 66. contentNode.appendChild(scriptNode); Dec 11, 2013 at 13:53
  • You shouldn't need to recreate every script tag on the page, calling the Nivo slider init function on the new elements should be enough, refer to the Nivo slider docs for how to do that
    – Tom J Nowell
    Oct 26, 2015 at 18:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.