简体   繁体   English

如何使用`link_to`和Rails 5上的Turbolinks使Twitter widget.js在首页加载时正常运行

[英]How to make Twitter widget.js run properly on first page load using `link_to` with Turbolinks on Rails 5

I'm trying to include the twitter widget script for better performance of displaying embedded tweets in my Rails 5 application (code given here: https://dev.twitter.com/web/javascript/loading ) 我试图包括twitter小部件脚本,以提高在Rails 5应用程序中显示嵌入式tweet的性能(此处提供的代码: https : //dev.twitter.com/web/javascript/loading

function twitterWidget() {
      alert("twitter");
      window.twttr = (function (d, s, id) {
        var t, js, fjs = d.getElementsByTagName(s)[0];
        if (d.getElementById(id)) return;
        js = d.createElement(s); js.id = id;
        js.src= "https://platform.twitter.com/widgets.js";
        fjs.parentNode.insertBefore(js, fjs);
        return window.twttr || (t = { _e: [], ready: function (f) { t._e.push(f)  }  });

  }(document, "script", "twitter-wjs"));
};
$(document).on('turbolinks:load', twitterWidget);

This works fine unless on first page visit (the script runs - thus showing me the alert - but the tweet is not properly displayed). 除非在第一页访问(脚本运行-这样向我显示警报-但该推文未正确显示),否则此方法工作正常。

Edit: The code above will work if I directly call the page but not when I use a link_to from another page. 编辑:如果我直接调用页面,但是当我使用另一个页面的link_to时,上面的代码将起作用。 Then it only works on refreshing the same page again. 然后,它仅适用于再次刷新同一页面。

I disabled Turbolinks and it works fine, however I want to keep using Turbolinks for faster page loading. 我禁用了Turbolinks,并且工作正常,但是我想继续使用Turbolinks来加快页面加载速度。

Is there a solution for this by selectively disabling turbolinks for just this widget? 通过有选择地仅禁用此小部件的涡轮链接,是否有解决方案?

Thanks for your help! 谢谢你的帮助!

According to Turbolinks docs turbolinks:load event runs once on initial page loading but not after page changes. 根据Turbolinks文档, turbolinks:load事件在初始页面加载时运行一次,但在页面更改后不会运行。

To run it on every page you can try 要在每个页面上运行它,您可以尝试

document.addEventListener("turbolinks:load", twitterWidget)

Hope it will help! 希望对您有所帮助!

Thanks to help by a colleague I found the solution to the problem. 感谢同事的帮助,我找到了解决问题的方法。

The main issue is that turbolinks doesn't run content in the head so one needs to insert the twitter script into the body of the page. 主要问题是turbolinks不在头部运行内容,因此需要将twitter脚本插入页面主体。 Changing the script like the following has worked for me: 像下面这样更改脚本对我有用:

function twitterWidget() {
    window.twttr = (function (d, s, id) {
        var t, js, fjs = $("body");
        if (d.getElementById(id)) return;
        js = d.createElement(s); js.id = id;
        js.src= "https://platform.twitter.com/widgets.js";
        $(fjs).append(js, fjs);
        return window.twttr || (t = { _e: [], ready: function (f) { t._e.push(f)  }  });
    }(document, "script", "twitter-wjs"));
};
$(document).on('turbolinks:load', twitterWidget);

Hope this will help someone! 希望这会对某人有所帮助!

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

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