邮递窗口未定义
我正在尝试从FormStack中实现一种形式解决方案,以进入我的React/Gatsby网站。为此,我需要使用postscribe
注入外部脚本。我已经跟随了其他几个人,并且在本地有一个工作版本,其中包含以下代码:
import postscribe from postscribe
...
useEffect(() => {
if (advRef) {
if (window !== undefined) {
postscribe(
advRef.current,
"<script src=...myformurl...><\/script>",
)
}
}
}, [advRef])
...
<div id="adv" ref={advRef}></div>
但是,这在生产中不起作用,并且会抛出reference> referenceerror:window
错误。我之前已经遇到过这个问题,如您所见,我尝试了IF窗口是定义的块,但这似乎没有帮助。
我在另一个问题中看到了在这里这与<<代码> postscribe 已导入。但是我似乎也无法在这里解决解决方案。
I am trying to implement a form solution from formstack into my React/Gatsby website. To do so, I need to use postscribe
to inject an external script. I've followed a couple other people and I have a working version locally with the following code:
import postscribe from postscribe
...
useEffect(() => {
if (advRef) {
if (window !== undefined) {
postscribe(
advRef.current,
"<script src=...myformurl...><\/script>",
)
}
}
}, [advRef])
...
<div id="adv" ref={advRef}></div>
However, this does not work in production and it throws a ReferenceError: window is not defined
error. I have run into this before and as you can see I tried the if window is defined block, but that doesn't seem to help.
I saw in another question here that this has to do with how postscribe
is imported. But I can't seem to get the solution here to work either.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我需要一种使用脚本标签加载脚本的方法,而不是下载节点软件包。这篇文章的答案有点了,但是据我所知,当Postscribe试图更改页面时,它与此有关。 idk为什么以这种方式有效,但我在盖茨比项目中做了以下内容。
gatsby-plugin-load-script
I needed a way to load the script using a script tag rather downloading the node package. The answer to this post goes into it a bit, but as far as I understand it has something to do with when postscribe tries to make changes to the page. Idk why this way works but I did the following in my Gatsby project.
gatsby-plugin-load-script
尝试将检查从
window!== Undfeined
更改为 type> typeof window!==“ undefined” :Try to change your checking from
window !== undefined
totypeof window !== "undefined"
: