access javascript ckeditor object within an iframe

2019-09-17 10:31发布

问题:

Lets say I have a page with an iframe and within the iframe I have a ckeditor instance that I want to destroy from the containing page.

I would usually try something like this:

var iframe_document = document.getElementById("iframe_id").contentWindow.document;
for(var i in iframe_document.CKEDITOR.instances)
  iframe_document.CKEDITOR.instances[i].destroy();

However it appears that the ckeditor instance can not be accessed this way. Is it possible to destroy the instance from outside the document similar to this?

To clarify the exact error is "cannot read property 'instances' of undefined"

回答1:

Global variables belong to the window, not to the document so try this:

var iframe_CKEDITOR = document.getElemenyById("iframe_id").contentWindow.CKEDITOR;
for(var i in iframe_CKEDITOR.instances)
  iframe_CKEDITOR.instances[i].destroy();