简体   繁体   中英

Call non-global javascript function from iframe

Hi I am calling a few javascript functions from a same domain iframe by using the parent.myFunction() method. The only problem with this is that the functions seem to need to be in the global scope to be accessed this way. I would like to put all my functions in jQuery's document.ready wrapper function

Can I access functions within jQuery's document.ready wrapper from an iframe somehow? I know it is a scope issue, but how would I access myFunction if it were within jQuery's document.ready wrapper from a same domain iframe?

Thanks!

No, you can't. Just put them outside the local scope of $.ready , preferably namespaced to avoid global scope pollution.

If you would want to define them on document ready, you still can put them in the global scope:

jQuery(function($) {
    window.someFunc = function(){ ... };
    // or better
    window.namespace = {
        func: function(){ ... }
    };
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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