简体   繁体   中英

Access JavaScript object in parent from iframe

I am creating an TinyMCE plugin which uses an iframe. Withing the iframe defined by iframe.html, how do I access parentObject? I've tried window.top.parentObject , but no luck.

tinymce.PluginManager.add('image', function(editor, url) {

    var parentObject={'xxx':123};

    editor.addButton('image', {
        icon: 'image',
        onclick: function(){
            var tinyEditor=editor.windowManager.open({
                html: '<iframe src=iframe.html" id="plugin-id"></iframe>'
            });
        }
    });

});

A function body is a scope, and you're defining the variable inside a function. Move var parentObject to global scope and access it with window.top.parentObject or window.parent.parentObject .

You're not able to access this object, because it is enclosed by your function(editor, url) {...} , whereas withing iframe you can access only parent window and all it's variables. The only way is to move parentObject to global context.

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