Login Or Sign up

How to initiate designMode of iframe?

474 Views Asked by At

I'm trying to implement a simple iframe designmode I initiated in javascript into React. My pure js code:

<body>
<div id="textEditor">
    <button id="action" title="Bold"><b>Click me</b></button>
    <div id="richTextArea"></div>
        <iframe id="theWYSIWYG" name="theWYSIWYG" frameborder="0"></iframe>
</div>

<script>
    window.addEventListener("load", function () {
        var editor = theWYSIWYG.document;
        editor.designMode = 'on';
</script>

This is working of course. But trying in React:

class ExamCreate extends React.Component {
constructor(props) {
    super(props);
    this.examBody = null
}
state = {
    modalStage: 0,
    newExamName: '',
    newExamDescription: '',
    newExamTime: '',
}
componentDidUpdate = () => {
        var a = this.examBody.document;
        a.designMode = 'on';
}

and jsx:

<iframe ref={examBody => this.examBody = examBody} 
        id="examBody" name="examBody" frameBorder="0"></iframe>

But I cannot access the ref's document at all. How can I setup the designmode of the iframe ref?

4

There are 4 best solutions below

1
kenodek On

I'm new to React and this is confusing to me why don't you wrtie this.state inside of a constructor?

0
sir-haver On

I managed to solve it by adding the contentWindow property like so:

let editor = this.examBody.contentWindow.document;
editor.designMode = 'on'

I don't know why different in Reacat and why you cannot reach the document directly, hope it can help someone...

0
Ramazan Aliskhanov On

I had the same problem. I solved within body tag outside of React:

  <body onLoad='enableEditor()'>

and before body closing tag i have a script

<script type='text/javascript'>

      function enableEditor () {
          todoEditor.document.designMode = 'On'
          todoEditor.focus();
    }

</script>

todoEditor is a name of my (iframe name='todoEditor')

1
Chijioke Peter On

useEffect(() => {
    theWYSIWYG.document.designMode = 'on';
  }, []);
<div id="textEditor">
    <button id="action" title="Bold"><b>Click me</b></button>
    <div id="richTextArea"></div>
        <iframe id="theWYSIWYG" name="theWYSIWYG" frameborder="0"></iframe>
</div>