0

有什么办法可以将iframe生成的 with添加jotform到 React 组件中?

这是 iframe 的代码:

<iframe
  id="JotFormIFrame-81003857231348"
  onload="window.parent.scrollTo(0,0)"
  allowtransparency="true"
  allowfullscreen="true"
  allow="geolocation; microphone; camera"
  src="https://form.jotformeu.com/81003857231348"
  frameborder="0"
  style="width: 1px;
  min-width: 100%;
  height:539px;
  border:none;"
  scrolling="no"
>
</iframe>
<script type="text/javascript">
  var ifr = document.getElementById("JotFormIFrame-81003857231348");
  if(window.location.href && window.location.href.indexOf("?") > -1) {
    var get = window.location.href.substr(window.location.href.indexOf("?") + 1);
    if(ifr && get.length > 0) {
      var src = ifr.src;
      src = src.indexOf("?") > -1 ? src + "&" + get : src  + "?" + get;
      ifr.src = src;
    }
  }
  window.handleIFrameMessage = function(e) {
    var args = e.data.split(":");
    if (args.length > 2) { iframe = document.getElementById("JotFormIFrame-" + args[(args.length - 1)]); } else { iframe = document.getElementById("JotFormIFrame"); }
    if (!iframe) { return; }
    switch (args[0]) {
      case "scrollIntoView":
        iframe.scrollIntoView();
        break;
      case "setHeight":
        iframe.style.height = args[1] + "px";
        break;
      case "collapseErrorPage":
        if (iframe.clientHeight > window.innerHeight) {
          iframe.style.height = window.innerHeight + "px";
        }
        break;
      case "reloadPage":
        window.location.reload();
        break;
      case "loadScript":
        var src = args[1];
        if (args.length > 3) {
            src = args[1] + ':' + args[2];
        }
        var script = document.createElement('script');
        script.src = src;
        script.type = 'text/javascript';
        document.body.appendChild(script);
        break;
      case "exitFullscreen":
        if      (window.document.exitFullscreen)        window.document.exitFullscreen();
        else if (window.document.mozCancelFullScreen)   window.document.mozCancelFullScreen();
        else if (window.document.mozCancelFullscreen)   window.document.mozCancelFullScreen();
        else if (window.document.webkitExitFullscreen)  window.document.webkitExitFullscreen();
        else if (window.document.msExitFullscreen)      window.document.msExitFullscreen();
        break;
    }
    var isJotForm = (e.origin.indexOf("jotform") > -1) ? true : false;
    if(isJotForm && "contentWindow" in iframe && "postMessage" in iframe.contentWindow) {
      var urls = {"docurl":encodeURIComponent(document.URL),"referrer":encodeURIComponent(document.referrer)};
      iframe.contentWindow.postMessage(JSON.stringify({"type":"urls","value":urls}), "*");
    }
  };
  if (window.addEventListener) {
    window.addEventListener("message", handleIFrameMessage, false);
  } else if (window.attachEvent) {
    window.attachEvent("onmessage", handleIFrameMessage);
  }
  </script>

我尝试仅使用脚本创建一个新的 js 文件并将其加载到 react 组件中,但它不起作用并出现以下错误:

第 14 行:'iframe' 未定义
第 14 行:'iframe' 未定义
第 15 行:'iframe' 未定义
第 18 行:'iframe' 未定义
第 21 行:'iframe' 未定义
第 24 行:'iframe '未定义
第25行:'iframe'未定义
第50行:'iframe'未定义
第50行:'iframe'未定义
第52行:'iframe'未定义
第56行:'handleIFrameMessage'未定义
行58: 'handleIFrameMessage' 未定义

import React from 'react';

export default class ScriptServices extends React.Component {
  constructor(props) {
    super(props);
  }

  componentDidMount() {
    const s = document.createElement('script');
    s.type = 'text/javascript';
    s.async = true;
    s.src = "./scriptServices.js";
    this.instance.appendChild(s);
  }  

  render() {
    return (
      <iframe
        id="JotFormIFrame-81003857231348"
        onload="window.parent.scrollTo(0,0)"
        allowtransparency="true"
        allowfullscreen="true"
        allow="geolocation; microphone; camera"
        src="https://form.jotformeu.com/81003857231348"
        frameborder="0"
        style="width: 1px;
        min-width: 100%;
        height:539px;
        border:none;"
        scrolling="no"
      >
      </iframe>
    )
  }
}

我只是想将 jotform 添加到我用 react 构建的网络应用程序中,但我无法让它工作......

4

1 回答 1

1

我制作了一个组件,可以轻松地将 JotForm.com 表单嵌入到基于 ReactJS 的网站或应用程序中。它支持 NextJS / Gatsby 之类的 SSR 框架。

如果需要,您还可以访问一些回调,例如 onSubmit。

你可以在这里访问它:https ://npmjs.com/package/jotform-react

示例用法:

import JotFormReact 'jotform-react';

const YourApp = () => {
    return (<div>
      ...Your App...
      <JotFormReact
        formURL="https://form.jotform.com/211272589254055"
      />
    </div>)
}
于 2021-05-21T12:21:01.960 回答