How can I access functions wrapped in $ (document) .ready (function () {}) iframe?

So, I have an iframe that has some functions wrapped in $ (document) .ready (function () {}), like this.

<script>
    $(document).ready(function(){
        function foo(){
            // do some stuff
        }
    });
</script>

I know that I can access the DOM with something like $ ('iframe'). contents (). find ('something'), but can I access the foo () function from above?

+5
source share
2 answers

iframe , ( "" ). , iframe, . , iframe-:

$(document).ready(function() {
    function func1() {
        alert("Function in iframe");
    }

    parent.iframeFunctions = {
        test: func1
    }
});

, . jquery, iframe , DOM , iframeFunctions - undefined . :

$(window).load(function() {
    iframeFunctions.test();
});

iframe. , , iframe . , , . jquery, , , jquery iframe:

$("iframe")[0].contentWindow.$("#divInIframe")

, iframe , declartions:

var func1 = function() {
    alert("Function in iframe");
}

$(document).ready(function() {
    func1();
});

( , ):

$(window).load(function() {
    $("iframe")[0].contentWindow.func1();
});
+4

. foo (, i - !).

iframe:

1) foo global.

$(document).ready(function(){
    foo = function(){
        //this alternate syntax declares a global function foo
        //it is very evil so be sure you really need this
        ...
    };
})

foo ,

...

function foo(){
    ...
};

$(document).ready(function(){

})

2) foo , ( , )

$(document).ready(function(){
    function foo(){
        //this alternate syntax declares a global function foo
        //it is very evil so be sure you really need this
        ...
    };

    function_that_uses_foo();
})

function_that_uses_foo(){
    foo(); //foo is not on cope, this doesnt work!
}

$(document).ready(function(){
    function foo(){
        //this alternate syntax declares a global function foo
        //it is very evil so be sure you really need this
        ...
    };

    function_that_uses_foo(foo);
})

function_that_uses_foo(his_foo){
    his_foo();
}

iframe, - , .

0

All Articles