0

I have this url to call the taxonomy.php by pressing the button with the code beneath to show filtered posts:

http://myurl/?meta_key=post_views_count&orderby=meta_value&order=ASC

This is the JS I am using:

    $(document).ready(function(){
$.ajaxSetup({cache:false});
$("#hot a").click(function(){

    var post_id = $(this).attr("rel")
    $(".postbox_wrapper").html('<span class="filter_posts"><img src="<?php bloginfo ('template_directory'); ?>/images/287.gif"></span>');
    $(".postbox_wrapper").load(jQuery(this).attr("href") + " .postbox_wrapper")
    return false;
});

});

The call works perfectly - the content show up without pagereload - as I wish.

The problem is, that JS is not loaded on the ajaxloaded pages and I really don't get it to load. I tried out many versions, I found on stackexchange, but nothing worked out.

For example this needs to be loaded into the ajaxed call: http://myname.disqus.com/count.js?ver=3.4.1

Has anybody an idea?

Thanky you very much.

AD

2
  • I think this is maybe beyond the scope of WordPress Answers, as it's not specific to WordPress. You're not loading a page via AJAX, you're loading specific elements from one DOM into an existing DOM.
    – Milo
    Jul 22, 2012 at 14:56
  • Are you saying you want JS to load on the actual AJAX call? If for some reason that's the case, you might need to dig into server-side vs. client-side processing. Jul 23, 2012 at 1:58

2 Answers 2

3

Try this code in your complete callback of the load function:

$(".postbox_wrapper").load(
    jQuery(this).attr("href") + " .postbox_wrapper",
    function(response, status, xhr) { // complete callback

        // create a empty div
        var div = document.createElement('div');
        // fill div with response
        div.innerHTML = response;
        // take correct part of the response
        var ref = $($(div).find('.postbox_wrapper').html());
        // filter response for script tags
        ref.filter('script').each(function () {
            // execute the scripts
            $.globalEval(this.text || this.textContent || this.innerHTML || '');
        });
    }
});

I would also also recommend that you add a error message if the call fails. See example 2 on http://api.jquery.com/load/

I had a hard time to figure that out, I tried all the answers found on stackoverflow. Finally I found this german site. There you can see the full code: http://labor.99grad.de/flash/agentur/wiesbaden/jquery/script-tag-bei-ajax-response-ausfuhren/

2
  • Hi Xsonic - thank you very much for th reply. I tried your code. It seems to be exactly what I need. It's not working so far - where should I put the code exactly? Thank you!!
    – ad2003
    Jul 22, 2012 at 19:37
  • 1
    I edited the code above to make it cealrer
    – xsonic
    Jul 23, 2012 at 8:19
0

I ended up with this code:

$.ajaxSetup({cache:false});
$("#fokus a").click(function(){

    var post_id = $(this).attr("rel");
    $(".postbox_wrapper").html('<span class="filter_posts"><img src="<?php bloginfo ('template_directory'); ?>/images/287.gif"></span>');
    $(".postbox_wrapper").load((jQuery(this).attr("href") + " .postbox_wrapper"), function() {
    //$.globalEval('http://check.disqus.com/count.js?ver=3.4.1');
        $.getScript("http://check.disqus.com/count.js?ver=3.4.1", function() {
            //alert('first Load was performed.');
        },false);
    });
    //alert('second Load was performed.');
    return false;
    });

This works so far. Thank you for your help!

AD

Your Answer

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

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