Turbolinks Rails application Uncaught TypeError: Cannot read property 'parentNode'
Turbolinks Rails application Uncaught TypeError: Cannot read property 'parentNode'
我正在尝试在 Rails turbolinks 站点上的 Ruby 中使用 Freshdesk 小部件。
自从我开始使用这个小部件,在初始页面加载后导航到其他页面时,我的控制台中总是出现以下错误:
> VM2797 <widget_id>.js:1 Uncaught TypeError: Cannot read property
> 'postMessage' of null
> at Object.postMessage (VM2797 <widget_id>.js:1)
> at Object.widgetRenderComplete (VM2797 <widget_id>.js:1)
> at Object.handleMessage (VM2797 <widget_id>.js:1)
这是我的代码 在添加一个带有 id 的 div 之前(我在我的视图中用 ID 作为变量调用它的部分)
<script>
window.fwSettings={
'widget_id': <%= widget_id %>
};
!function(){if("function"!=typeof window.FreshworksWidget){var n=function(){n.q.push(arguments)};n.q=[],window.FreshworksWidget=n}}()
</script>
<script type='text/javascript' src='https://widget.freshworks.com/widgets/<%= widget_id %>.js' async defer></script>
查看示例
<%= render "shared/freshdesk_widget", widget_id: 60000003593 %>
我考虑过使用 eventListener turbolinks:before-cache,它会查找 div 并在缓存之前将其删除。
这是现在的样子
<script type='text/javascript'>
document.addEventListener("turbolinks:before-cache", function() {
const deleteable = document.getElementById("div_to_delete");
deleteable.parentNode.removeChild(deleteable);
})
</script>
<div id="div_to_delete">
<script>
window.fwSettings={
'widget_id': <%= widget_id %>
};
!function(){if("function"!=typeof window.FreshworksWidget){var n=function(){n.q.push(arguments)};n.q=[],window.FreshworksWidget=n}}()
</script>
<script type='text/javascript' src='https://widget.freshworks.com/widgets/<%= widget_id %>.js' async defer></script>
</div>
我遇到了两个错误
> VM49956:4 Uncaught TypeError: Cannot read property 'parentNode' of
> null
> at HTMLDocument.<anonymous> (<anonymous>:4:15)
> at
Object../node_modules/turbolinks/dist/turbolinks.js.e.dispatch
> (turbolinks.js:5)
> at r.notifyApplicationBeforeCachingSnapshot
(turbolinks.js:6)
> at r.cacheSnapshot (turbolinks.js:6)
> at r.cacheSnapshot (turbolinks.js:5)
> at r.<anonymous> (turbolinks.js:5)
> at turbolinks.js:5
和之前一样
> VM2797 <widget_id>.js:1 Uncaught TypeError: Cannot read property
> 'postMessage' of null
> at Object.postMessage (VM2797 <widget_id>.js:1)
> at Object.widgetRenderComplete (VM2797 <widget_id>.js:1)
> at Object.handleMessage (VM2797 <widget_id>.js:1)
提前致谢!
干杯
删除元素不会删除 DOM 中的小部件,因此,据我所知,您只需在 window.FreshworksWidget
变量时才初始化小部件未定义。
仅用以下行替换您的所有代码:
<script>
if (window.FreshworksWidget === undenfined ) {
window.fwSettings={
'widget_id': <%= widget_id %>
};
!function(){if("function"!=typeof window.FreshworksWidget){var n=function(){n.q.push(arguments)};n.q=[],window.FreshworksWidget=n}}()
}
</script>
我正在尝试在 Rails turbolinks 站点上的 Ruby 中使用 Freshdesk 小部件。
自从我开始使用这个小部件,在初始页面加载后导航到其他页面时,我的控制台中总是出现以下错误:
> VM2797 <widget_id>.js:1 Uncaught TypeError: Cannot read property
> 'postMessage' of null
> at Object.postMessage (VM2797 <widget_id>.js:1)
> at Object.widgetRenderComplete (VM2797 <widget_id>.js:1)
> at Object.handleMessage (VM2797 <widget_id>.js:1)
这是我的代码 在添加一个带有 id 的 div 之前(我在我的视图中用 ID 作为变量调用它的部分)
<script>
window.fwSettings={
'widget_id': <%= widget_id %>
};
!function(){if("function"!=typeof window.FreshworksWidget){var n=function(){n.q.push(arguments)};n.q=[],window.FreshworksWidget=n}}()
</script>
<script type='text/javascript' src='https://widget.freshworks.com/widgets/<%= widget_id %>.js' async defer></script>
查看示例
<%= render "shared/freshdesk_widget", widget_id: 60000003593 %>
我考虑过使用 eventListener turbolinks:before-cache,它会查找 div 并在缓存之前将其删除。
这是现在的样子
<script type='text/javascript'>
document.addEventListener("turbolinks:before-cache", function() {
const deleteable = document.getElementById("div_to_delete");
deleteable.parentNode.removeChild(deleteable);
})
</script>
<div id="div_to_delete">
<script>
window.fwSettings={
'widget_id': <%= widget_id %>
};
!function(){if("function"!=typeof window.FreshworksWidget){var n=function(){n.q.push(arguments)};n.q=[],window.FreshworksWidget=n}}()
</script>
<script type='text/javascript' src='https://widget.freshworks.com/widgets/<%= widget_id %>.js' async defer></script>
</div>
我遇到了两个错误
> VM49956:4 Uncaught TypeError: Cannot read property 'parentNode' of
> null
> at HTMLDocument.<anonymous> (<anonymous>:4:15)
> at
Object../node_modules/turbolinks/dist/turbolinks.js.e.dispatch
> (turbolinks.js:5)
> at r.notifyApplicationBeforeCachingSnapshot
(turbolinks.js:6)
> at r.cacheSnapshot (turbolinks.js:6)
> at r.cacheSnapshot (turbolinks.js:5)
> at r.<anonymous> (turbolinks.js:5)
> at turbolinks.js:5
和之前一样
> VM2797 <widget_id>.js:1 Uncaught TypeError: Cannot read property
> 'postMessage' of null
> at Object.postMessage (VM2797 <widget_id>.js:1)
> at Object.widgetRenderComplete (VM2797 <widget_id>.js:1)
> at Object.handleMessage (VM2797 <widget_id>.js:1)
提前致谢! 干杯
删除元素不会删除 DOM 中的小部件,因此,据我所知,您只需在 window.FreshworksWidget
变量时才初始化小部件未定义。
仅用以下行替换您的所有代码:
<script>
if (window.FreshworksWidget === undenfined ) {
window.fwSettings={
'widget_id': <%= widget_id %>
};
!function(){if("function"!=typeof window.FreshworksWidget){var n=function(){n.q.push(arguments)};n.q=[],window.FreshworksWidget=n}}()
}
</script>