execcommand not working in opera
-
24-10-2019 - |
Question
i have this code, execcommand for BOLD, and it is not working under opera. anybody have some idea why ? thanks
<script type="text/javascript">
function SetToBold () {
Editor.execCommand ('bold', false, null);
}
function start() {
frames.iView.document.designMode = "On";
var myIFrame = document.getElementById("iView");
browser = navigator.appName;
if (document.all) { //IE
var iframe_window = window.frames["iView"];
iframe_window.document.body.focus();
}
else { //Firefox
field = myIFrame.contentWindow.document.getElementsByTagName("body")[0];
setTimeout("field.focus()",250);
}
if (browser == "Netscape") {
Editor = myIFrame.contentWindow.document;
Editor.designMode = 'On';
Editor.body.contentEditable = 'true';
}
else if(browser == "Microsoft Internet Explorer") {
frames['iView'].document.designMode='On';
Editor = frames['iView'].document;
}
}
i call start function with body onload. it is ok firefox and ie, but doesnt work in opera. anybody knows about some restrictions of execcommand in opera ? thanks
Solution
In the quoted script, the variable "Editor" is given a value only inside the browser sniffing section. In Opera "Editor" will be undefined and hence trying to do "Editor.execCommand()" will throw an error.
Rather than
if (browser == "Netscape") {
Editor = myIFrame.contentWindow.document;
Editor.designMode = 'On';
Editor.body.co`enter code here`ntentEditable = 'true';
}
else if(browser == "Microsoft Internet Explorer") {
frames['iView'].document.designMode='On';
Editor = frames['iView'].document;
}
you should drop the browser sniffing and do something like
if ( myIFrame && myIFrame.contentWindow ) {
Editor = myIFrame.contentWindow.document;
Editor.designMode = 'On';
}
else if( frames['iView'] ) {
frames['iView'].document.designMode='On';
Editor = frames['iView'].document;
}
Hope that helps :)
OTHER TIPS
before giving an answer I want to be sure about what you are asking. The script is testing Netscape or IE. Opening dragonfly console, you can discover the values of your script. I have the feeling you will have difficulties with Webkit too.
>>> var browser = navigator.appName
undefined
>>> browser
"Opera"
ive noted that the newer versions of opera appear as NETSCAPE when you ask for it with navigator.appName. i remember that was not always so because i also got some older "ifs" around my source where i used the regular name. cant say since when this is, just saying.