execCommand give error
-
02-07-2021 - |
Вопрос
I create a new iframe then append it to the main window document and execute this javascript:
frame.contentDocument.execCommand('insertimage',0,'obama.jpg');
Where frame is the iframe element. Unfortunatey I get this error:
Error: Component returned failure code: 0x80004005 (NS_ERROR_FAILURE) [nsIDOMHTMLDocument.execCommand]
How can I enable execCommand() function in an iframe? Is there something missing?
Решение
The document needs to be editable. This can be done by either of the followin methods:
frame.contentDocument.designMode = 'on'; // = 'off'; to disable`
frame.contentDocument.body.contentEditable = true; // = false; to disable`
execCommand
ought to be used for editable elements only. Your error indicates that this is not the case. If you want to insert a new image in the document, just use:
var img = new Image();
img.src = 'http://example.com/obama.jpg';
frame.contentDocument.appendChild(img);
Не связан с StackOverflow