首页 > 解决方案 > 使用带有 rails turbolinks 的插件

问题描述

Helpscout 提供了一个 javascript 插件来将信标/小部件添加到站点底部,如下所示:

  <script>
  !function(e,o,n){
    window.HSCW=o,window.HS=n,n.beacon=n.beacon||{};var t=n.beacon;t.userConfig={},t.readyQueue=[],t.config=function(e){this.userConfig=e},t.ready=function(e){this.readyQueue.push(e)},o.config={
    docs:{enabled:!0,baseUrl:"https://xyz.helpscoutdocs.com/"},
   contact:{enabled:!0,formId:"xyz"}};var r=e.getElementsByTagName("script")[0],c=e.createElement("script");c.type="text/javascript",c.async=!0,c.src="https://djtflbt20bdde.cloudfront.net/",r.parentNode.insertBefore(c,r)
  }(document,window.HSCW||{},window.HS||{});
  HS.beacon.config({topArticles: true,poweredBy: false, topics: [{val: "help", label: "Help"}]});
  HS.beacon.ready(function() {
  HS.beacon.prefill({subject: "Support"});
});
  </script>

在带有 turbolinks 的 Rails 应用程序上,这会在第一个页面加载后的每个页面加载时生成 JS 错误:“未捕获的 TypeError:无法读取 null 的属性‘createElement’”。

如何重新配置​​这样的脚本以使用 tublinks?

标签: ruby-on-railsturbolinks

解决方案


你可以调用 javascriptpage:loadturbolinks:load

$(document).on('ready page:load', function() {
  // js code
});

或者

$(document).on('ready turbolinks:load', function() {
  // js code
});

推荐阅读