简体   繁体   English

在通过Fancybox弹出窗口打开的iFrame中调用javascript函数

[英]Calling a javascript function in iFrame opened with Fancybox popup

I'm trying to call a javascript function in a page opened in an iFrame using jQuery Fancybox. 我正在尝试使用jQuery Fancybox在iFrame中打开的页面中调用javascript函数。

            $(".testbox").fancybox({
                maxWidth    : 800,
                maxHeight   : 600,
                fitToView   : false,
                autoSize    : false,
                closeClick  : false,
                openEffect  : 'none',
                closeEffect : 'none',
                afterShow: function() {
                        //var frameID = $('#fancybox-frame')
                        //frameID.getParam("test");                         
                    }
            }); 

        $(".testbox").each(function() {
                var element = this;
                $(this).fancybox({
                    'titleFormat'   : function() {
                        var astring = '<span>' + element.id + '</span>';
                        alert(astring);
                    }
                });
            });

HTML: HTML:

    <a class="testbox fancybox.iframe" href="include/testFrame.html">Iframe</a>

The function in the testFrame.html I'm trying to call: 我正在尝试调用testFrame.html中的函数:

        function getParam(param) {              
            alert(param);               
        }

Normally I'd do this when calling functions in iFrame without using a Fancybox popup: 通常,在不使用Fancybox弹出窗口的情况下在iFrame中调用函数时,我会这样做:

        document.getElementById('myFrame').contentWindow.getParam();

but I don't know how to get the ID of the whole iFrame when using Fancybox. 但我不知道在使用Fancybox时如何获取整个iFrame的ID。 Any help is appreciated! 任何帮助表示赞赏!

Edit: I also tried this code which also didn't work, I get the error in IE: Object doesn't support this property or method. 编辑:我也尝试了此代码也无法正常工作,我得到IE中的错误:对象不支持此属性或方法。 I've made sure the page I'm loading has the function 我确定我要加载的页面具有功能

    var $f = $("#fancybox-frame");
    var fd = $f[0].document || $f[0].contentWindow.document; 
    fd.getparam("test");  

You should use: 您应该使用:

var $f = $("#fancybox-frame");
$f[0].contentWindow.getParam("test");

Reasons: 原因:

  • The method is globally declared, not as a method of the document object. 该方法是全局声明的,而不是作为document对象的方法声明的。
  • JavaScript is case-sensitive; JavaScript区分大小写; getParam is distinct from getparam . getParam是从不同getparam

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM