简体   繁体   English

Rails link_to 和 onClick with target=_blank 不能一起工作

[英]Rails link_to and onClick with target=_blank does not work together

I believe this should have quite an easy solution but I am not used to work with JavaScript.我相信这应该有一个非常简单的解决方案,但我不习惯使用 JavaScript。

I wanted to add the event handler to Google Analytics and did so by using their suggestion code:我想将事件处理程序添加到 Google Analytics,并通过使用他们的建议代码来实现:

<script>
var trackOutboundLink = function(url) {
   ga('send', 'event', 'outbound', 'click', url, {'hitCallback':
     function () {
     document.location = url;
     }
   });
}
</script>   

with each link link this:每个链接链接这个:

<a href="http://www.example.com" rel="nofollow" target="_blank" onClick="trackOutboundLink('http://www.example.com'); return false;">Example.com</a>

This works fine, Analytics-wise, but I have lost the possibility to open the link in a new window.这在分析方面工作正常,但我已经失去了在新窗口中打开链接的可能性。 I assume the JavaScript overrides that in some way, so I how do I make sure I can still use HTML options like target=_blank together with the onClick ?我假设 JavaScript 以某种方式覆盖了它,所以我如何确保我仍然可以将target=_blank等 HTML 选项与onClick一起使用? I really rather not want to handle opening new windows in JavaScript.我真的不想在 JavaScript 中处理打开新窗口。

Please try to use window.open()请尝试使用window.open()

<script>
  var trackOutboundLink = function(url) {
    ga('send', 'event', 'outbound', 'click', url, {'hitCallback':
     function () {
         window.open(url);
      }
      });
    }
</script>   

Please see this link请看这个链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM