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
3 Answers
Reset to default 12Add 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
版权声明:本文标题:Rails 4: How to add external javascript file from other site in a specific page - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738674323a2106173.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论