Search code examples
operaexeccommand

execcommand not working in opera


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 :)