我有什么方法可以将iframe
生成的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行:&#39; iframe&#39;未定义
第14行:&#39; iframe&#39;未定义
第15行:&#39; iframe&#39;未定义
第18行:&#39; iframe&#39;未定义
第21行:&#39; iframe&#39;未定义
第24行:&#39; iframe&#39;未定义
第25行:&#39; iframe&#39;未定义
第50行:&#39; iframe&#39;未定义
第50行:&#39; iframe&#39;未定义
第52行:&#39; iframe&#39;未定义
第56行:&#39; handleIFrameMessage&#39;未定义
第58行:&#39; handleIFrameMessage&#39;未定义
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添加到我的网站应用程序中,但是我无法让它工作......
答案 0 :(得分: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>)
}