如何从iframe中使用jQuery调用父页面的功能? [英] How to call function on parent page from iframe using jQuery?

查看:145
本文介绍了如何从iframe中使用jQuery调用父页面的功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个上传表单的职位,以一个隐藏的iframe。我试图调用从iframe的父页面上的功能,但我得到的错误top.stopUpload不是一个函数。

什么是做这种正确的方法是什么?

父页面:

  $(文件)。就绪(函数(){

    $('#document_upload)。递交(函数(){

        $('#upload_progress)显示()。

     });

    功能stopUpload(成功){

        如果(成功== 1){
            $('#结果,window.parent.document)的.html(
            '<跨度类=味精>!该文件已成功上传< \ / SPAN>');
        }

        其他 {
            $('#结果,window.parent.document)的.html(
            '<跨度类=emsg>有文件上传过程中出现错误<!\ / SPAN>');
        }

        $('#upload_progress)隐藏()。

        返回true;

    }

})
 

IFRAME:

  $(文件)。就绪(函数(){

   top.stopUpload(小于?PHP的回声$结果;?>);

}
 

解决方案

stopUpload()函数的作用范围是传递给匿名函数$(文件)。就绪()

您将需要确保其范围是可见的 IFRAME 。做到这一点的方法之一是分配的功能窗口,在浏览器中的全局对象。

另一种方法是将在全球code创建函数,匿名函数之外。

I have an upload form that posts to a hidden iframe. I am attempting to call a function on the parent page from the iframe, but am getting the error "top.stopUpload is not a function".

What is the correct way to do this?

PARENT PAGE:

$(document).ready(function() {

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

        $('#upload_progress').show();

     });

    function stopUpload(success){

        if (success == 1){
            $('#result', window.parent.document).html(
            '<span class="msg">The file was uploaded successfully!<\/span>');
        }

        else {
            $('#result', window.parent.document).html(
            '<span class="emsg">There was an error during file upload!<\/span>');
        }

        $('#upload_progress').hide();

        return true;

    }

})

IFRAME:

$(document).ready(function() {

   top.stopUpload(<?php echo $result; ?>);

}

解决方案

Your stopUpload() function is scoped to the anonymous function you pass to $(document).ready().

You will need to ensure its scope is visible to the iframe. One way to do that would be to assign the function to window, the global object in a browser.

Another method would be to create the function in the global code, outside of that anonymous function.

这篇关于如何从iframe中使用jQuery调用父页面的功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆