6

I need to use a third part embed code for a form. I'm trying to return it in a component, but it's obviously not working. How can I set this one up?

class Form extends Component {

  render() {

    return (
      <div>
        <div id="apply_form">
          <a name="form" id="formAnchor"></a>
          <script type="text/javascript" src="https://fs22.formsite.com/include/form/embedManager.js?1605047845"></script>
          <script type="text/javascript">
          EmbedManager.embed({
            key: "key",
            width: "100%",
            mobileResponsive: true
          });
          </script>
        </div>
      </div>
    );
  }
}

export default Form;

1 Answer 1

2

Unfortunately, there's not a really clean way of doing this, short of reaching into the DOM to insert the external script, then running the embed code directly.

Here's how I'd go about this:

class Form extends Component {

  componentDidMount() {
    const script = document.createElement('script');
    script.setAttribute(
      'src', 
      'https://fs22.formsite.com/include/form/embedManager.js?1605047845');
    script.addEventListener('load', () => {
      EmbedManager.embed({
        key: "key",
        width: "100%",
        mobileResponsive: true
      });
    });
    document.body.appendChild(script);
  }

  render() {
    return (
      <div>
        <div id="apply_form">
          <a name="form" id="formAnchor"></a>
        </div>
      </div>
    );
  }
}

export default Form;
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you so much! This worked! and cleared things up for me.
I have an issue now - when I add it to the page with import and then <Form/> the console shows an error - Uncaught TypeError: Cannot read property 'parentNode' of null - any idea why?
No idea; it's probably specific to this embed script. You could try swapping the addEventListener and appendChild lines; perhaps it's trying to read the DOM positioning of the <script> element.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.