0

When closing the Thickbox (by clicking on the cross), I am invoking the following function, by hooking it to the tb_unload action. However, the action is being called twice for some reason, and I don't know why. Is there a way to stop the functino running more than once? Or better yet, finding out what is causing the action to be called twice? Thanks.

jQuery(document).ready(function($){

    $(window).bind('tb_unload', function(){

        /** Set the params for passing to the AJAX function */
        data = {
            security: $('input#_wpnonce', '#dd-options-edit-footer-page').val(),
            action: 'update-footer-images',
        };

        $.post('admin-ajax.php', data, function(response){

            alert(response);

            /** Place the image src in the hidden image, and then show the image */
            $('#image-preview', '#dd-optinos-footer-page').html(response);

        });

    });

});
  • Have you used any development tools to debug the JavaScript? (Firebug, Chrome Developer Tools, etc)? – Jared Cobb Jan 3 '13 at 19:24
  • Yes, I use Firebug, but I'm unaware that you are able to use it for this. How would I go about finding this out in Firebug? – David Gard Jan 4 '13 at 9:10
  • One of Firebug's more powerful features is the Script panel. If you enable that panel you'll have access to all of the JavaScript being used on that page. But the real power comes from setting breakpoints and "debugging" your scripts while they run. This simply means being able to tell the browser to "pause" in the middle of running the code and allow you to "step" along and "watch" variable values. This tutorial is well worth the effort and once you learn to use the debugger you'll save yourself a lot of frustration and time. netmagazine.com/tutorials/javascript-debugging-beginners – Jared Cobb Jan 4 '13 at 13:43
  • Thanks very much for the link, I will review as soon as I get the chance. – David Gard Jan 4 '13 at 15:38
1

A very basic fix would be to use one() instead of bind(). But why the function is called twice is still open. Ask your browser’s JavaScript inspector.

| improve this answer | |
  • Thanks, .one() is working. I do use Firebug, but I'm not familier with using it for this reason, so am strugging a little bit... – David Gard Jan 4 '13 at 13:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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