admin管理员组

文章数量:1208155

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

<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:

//= 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. .js in a specifc page of my site. Suppose I want to add this external js file ONLY in a specifc page And 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.

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

<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:

//= 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.js in a specifc page of my site. Suppose I want to add this external js file ONLY in a specifc page http://mysite.com/profile And 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.

Share Improve this question edited Sep 17, 2013 at 21:40 JVK asked Sep 17, 2013 at 21:29 JVKJVK 3,9128 gold badges47 silver badges69 bronze badges 2
  • Does it absolutely need to be in the header? – Matt Commented Sep 17, 2013 at 21:58
  • @Matt Yes it absolutely need to be in header – JVK Commented Sep 17, 2013 at 22:19
Add a comment  | 

3 Answers 3

Reset to default 12

Add a content block in your layout:

layout.html.erb:

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

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

profile.html.erb:

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

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

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

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));

本文标签: Rails 4 How to add external javascript file from other site in a specific pageStack Overflow