user1032531
user1032531

Reputation: 26331

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>'
            });
        }
    });

});

Upvotes: 0

Views: 1789

Answers (2)

uzer
uzer

Reputation: 160

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.

Upvotes: 1

Dark Falcon
Dark Falcon

Reputation: 44201

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.

Upvotes: 3

Related Questions