阻止iframe回发从父级调用jQuery'ready'函数

时间:2022-09-01 16:00:20

I have an an iframe within a jQuery tab layout. I am using the iframe to upload an image, to simulate a AJAX file upload. It seems whenever my iframe postbacks, it causes the parent page's $(document).ready to execute, resetting the current tab I am positioned on.

我在jQuery选项卡布局中有一个iframe。我正在使用iframe上传图像,以模拟AJAX文件上传。似乎每当我的iframe回发时,它都会导致父页面的$(document).ready执行,重置我所定位的当前选项卡。

Any ideas on how to prevent this?

有关如何防止这种情况的任何想法?

    function uploadDone() {

    var data = eval("("+ $('#upload_target').contents().find('body').html() +")");
    if(data.success) {
       $('#upload_summary').append("<img src='" + data.full_path + "' />");
       $('#upload_summary').append("<br />Size: " + data.file_size + " KB");
    }
    else if(data.failure) { //Upload failed - show user the reason.
        alert("Upload Failed: " + data.failure);
    }
}

$(document).ready(function() {

    $('#image_uploader').submit(function() {

        if($.browser.safari || $.browser.opera) {

            $('#upload_target').load(function(){
                setTimeout(uploadDone, 0);
            });

            var iframe = document.getElementById('upload_target');
            var iSource = iframe.src;
            iframe.src = '';
            iframe.src = iSource;

        } else {

            $('#upload_target').load(uploadDone);
        }
    });

});

1 个解决方案

#1


A flag makes sense (as what SolutionYogi mentioned). If the document.ready in the parent frame is only required to execute once, set a global flag to a value which you can check when it got fired again.

旗帜是有道理的(正如SolutionYogi所提到的那样)。如果父框架中的document.ready只需要执行一次,则将全局标志设置为一个值,您可以在再次触发时检查该值。

Another way is to unbind document.ready after executing it. E.g. $(document).unbind('ready')

另一种方法是在执行后取消绑定document.ready。例如。 (文档)$ .unbind( '准备好')

#1


A flag makes sense (as what SolutionYogi mentioned). If the document.ready in the parent frame is only required to execute once, set a global flag to a value which you can check when it got fired again.

旗帜是有道理的(正如SolutionYogi所提到的那样)。如果父框架中的document.ready只需要执行一次,则将全局标志设置为一个值,您可以在再次触发时检查该值。

Another way is to unbind document.ready after executing it. E.g. $(document).unbind('ready')

另一种方法是在执行后取消绑定document.ready。例如。 (文档)$ .unbind( '准备好')