Rails 4:如何在特定页面中添加来自其他站点的外部 javascript 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18860226/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 13:28:19  来源:igfitidea点击:

Rails 4: How to add external javascript file from other site in a specific page

javascriptruby-on-railsasset-pipelineruby-on-rails-4sprockets

提问by JVK

I am using turbolink (rails4) and following js link gets generated by application.js file in my pages header section

我正在使用 turbolink (rails4) 并在我的页面标题部分中通过 application.js 文件生成以下 js 链接

<script data-turbolinks-track="true" src="/assets/jquery.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/jquery_ujs.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/turbolinks.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/global.js?body=1"></script>

My application.js looks something like:

我的 application.js 看起来像:

//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require_tree .
//= require bootstrap.min.js
//= require respond.min.js

I want to add an external javascript file from OTHER site e.g. http://otherdomain.com/xyz.jsin a specifc page of my site. Suppose I want to add this external js file ONLY in a specifc page http://mysite.com/profileAnd I want to add this js file ONLY in header part of the page. So how can I do that? Please don't suggest to save that external file locally as that is not an option for me.

我想从其他站点添加一个外部 javascript 文件,例如http://otherdomain.com/xyz.js在我站点的特定页面中。假设我只想在特定页面http://mysite.com/profile 中添加这个外部 js 文件并且我只想在页面的标题部分添加这个 js 文件。那我该怎么做呢?请不要建议在本地保存该外部文件,因为这对我来说不是一个选择。

回答by Ross Allen

Add a content block in your layout:

在您的布局中添加一个内容块:

layout.html.erb:

layout.html.erb

...
<head>
  <%= yield(:header) %>
</head>
...

In the one template that requires the JS file, render it into that block:

在需要 JS 文件的一个模板中,将其渲染到该块中:

profile.html.erb:

profile.html.erb:

<% content_for(:header) do %>
  <script src="http://otherdomain.com/xyz.js"></script>
<% end %>

回答by trh

As cool as turbolinks are, I find myself with more headaches than before they existed. I also inject page specific css or js in certain special circumstances it exists in the header. It might be hacky, but I put it in the layout with a condition using the current_page? helper

尽管 turbolinks 很酷,但我发现自己比它们存在之前更头疼。在某些特殊情况下,我还会注入页面特定的 css 或 js,它存在于标题中。它可能是hacky,但我使用current_page将它放在带有条件的布局中?帮手

 = javascript_include_tag "whatever.com/external.js" if ( current_page?(:controller => "users" ) && current_page?(:action => "index" ) )

回答by Jonathan Allard

A solution I'm looking at presently is injecting the script dynamically:

我目前正在考虑的一个解决方案是动态注入脚本

(function(d, script) {
    script = d.createElement('script');
    script.type = 'text/javascript';
    script.async = true;
    script.onload = function(){
        // remote script has loaded
    };
    script.src = 'http://www.google-analytics.com/ga.js';
    d.getElementsByTagName('head')[0].appendChild(script);
}(document));
(function(d, script) {
    script = d.createElement('script');
    script.type = 'text/javascript';
    script.async = true;
    script.onload = function(){
        // remote script has loaded
    };
    script.src = 'http://www.google-analytics.com/ga.js';
    d.getElementsByTagName('head')[0].appendChild(script);
}(document));