单击链接

时间:2017-11-29 16:05:53

标签: javascript ruby-on-rails ruby-on-rails-5 turbolinks

我正在尝试在我的网站上安装Comodo的TrustLogo ...所以,我在<head>部分有这个:

<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
<script type="text/javascript"> //<![CDATA[ 
      var tlJsHost = ((window.location.protocol == "https:") ? "https://secure.comodo.com/" : "http://www.trustlogo.com/");
      document.write(unescape("%3Cscript src='" + tlJsHost + "trustlogo/javascript/trustlogo.js' type='text/javascript'%3E%3C/script%3E"));
      //]]>
</script>

问题在于,当我访问页面http://localhost:3000时,主页加载正常;

但是如果我尝试点击任何链接,页面就不会加载;

按F5或重新加载按钮,它可以正常加载

所以我尝试这样做:

<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
<%= javascript_include_tag 'https://secure.comodo.com/trustlogo/javascript/trustlogo.js', 'data-turbolinks-track': 'reload' %>

但这会导致同样的问题。

我也尝试更改位置,因此我转到application.js并添加以下内容:

document.addEventListener("turbolinks:load", function(){
  var tlJsHost = ((window.location.protocol == "https:") ? "https://secure.comodo.com/" : "http://www.trustlogo.com/");
  document.write(unescape("%3Cscript src='" + tlJsHost + "trustlogo/javascript/trustlogo.js' type='text/javascript'%3E%3C/script%3E"));
});

但这给了我以下HTML结果(空白页)

<html>
<head>
<script src="http://www.trustlogo.com/trustlogo/javascript/trustlogo.js" type="text/javascript"></script>
<style type="text/css" media="screen">#comodoTL {display:block;font-size:8px;padding-left:18px;}</style>
</head>
</html>

任何想法?

更新:

将代码移到<head>之外,就像@patkoperwas建议的那样,但页面仍然是空白的并且在控制台中出现以下错误:

  

turbolinks.self-1d1fddf91adc38ac2045c51f0a3e05ca97d07d24d15a4dcbf705009106489e69.js体= 1:6   未捕获的TypeError:无法读取null的属性'querySelector'       at n.t.SnapshotRenderer.n.findFirstAutofocusableElement(turbolinks.self-1d1fddf91adc38ac2045c51f0a3e05ca97d07d24d15a4dcbf705009106489e69.js?body = 1:6)       at n.t.SnapshotRenderer.n.focusFirstAutofocusableElement(turbolinks.self-1d1fddf91adc38ac2045c51f0a3e05ca97d07d24d15a4dcbf705009106489e69.js?body = 1:6)       at turbolinks.self-1d1fddf91adc38ac2045c51f0a3e05ca97d07d24d15a4dcbf705009106489e69.js?body = 1:6       在n.t.Renderer.t.renderView(turbolinks.self-1d1fddf91adc38ac2045c51f0a3e05ca97d07d24d15a4dcbf705009106489e69.js?body = 1:6)       at n.t.SnapshotRenderer.n.render(turbolinks.self-1d1fddf91adc38ac2045c51f0a3e05ca97d07d24d15a4dcbf705009106489e69.js?body = 1:6)       在Function.t.Renderer.t.render(turbolinks.self-1d1fddf91adc38ac2045c51f0a3e05ca97d07d24d15a4dcbf705009106489e69.js?body = 1:6)       在e.t.View.e.renderSnapshot(turbolinks.self-1d1fddf91adc38ac2045c51f0a3e05ca97d07d24d15a4dcbf705009106489e69.js?body = 1:6)       在e.t.View.e.render(turbolinks.self-1d1fddf91adc38ac2045c51f0a3e05ca97d07d24d15a4dcbf705009106489e69.js?body = 1:6)       在r.t.Controller.r.render(turbolinks.self-1d1fddf91adc38ac2045c51f0a3e05ca97d07d24d15a4dcbf705009106489e69.js?body = 1:6)       在r。 (?turbolinks.self-1d1fddf91adc38ac2045c51f0a3e05ca97d07d24d15a4dcbf705009106489e69.js体= 1:6)

1 个答案:

答案 0 :(得分:0)

尝试移动脚本

<script type="text/javascript"> //<![CDATA[ 
      var tlJsHost = ((window.location.protocol == "https:") ? "https://secure.comodo.com/" : "http://www.trustlogo.com/");
      document.write(unescape("%3Cscript src='" + tlJsHost + "trustlogo/javascript/trustlogo.js' type='text/javascript'%3E%3C/script%3E"));
      //]]>
</script>

进入<body>而不是<head>。根据{{​​3}}:

  

Turbolinks每次呈现页面时评估页面<script>中的<body>个元素。您可以使用内联正文脚本来设置每页JavaScript状态或引导客户端模型。

正在发生的是该脚本在页面加载和刷新时首次运行和评估。在页面更改时,脚本不会重新运行(因为它在<head>中),所以没有任何反应。