Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 671
JVK Avatar asked Sep 17 '13 21:09

JVK


People also ask

How do you reference an external JavaScript file from a Web page?

To include an external JavaScript file, we can use the script tag with the attribute src . You've already used the src attribute when using images. The value for the src attribute should be the path to your JavaScript file. This script tag should be included between the <head> tags in your HTML document.

Can external JavaScript scripts can be linked into more than one page?

You can store one or more of your JavaScript scripts in a single . js file and access them from multiple HTML pages. Put the script in an external JavaScript file. If you have the script already inside your HTML page, remove all the JavaScript code inside the <script> tag and paste it into a separate file.

How do I link a JavaScript file in rails?

Copy external JavaScript libraries (such as jQuery plugins) to the vendor/assets/javascripts folder. Let the Rails asset pipeline combine them all in one minimized application. js file. List scripts in the app/assets/javascripts/application.

What is more appropriate way to include JavaScript as an external file?

Create external JavaScript file with the extension . js. After creating, add it to the HTML file in the script tag. The src attribute is used to include that external JavaScript file.

How to add JavaScript to a rails app?

The directory structure for JavaScript has changed to the app/javascript/packs/ folder. In that folder you will find the application.js file, which is just like the application.css file. It will be imported by default in the application.html.erb file when you create your new Rails app. The application.html.erb file will be used by all views.

How to include an external JavaScript file into an HTML file?

The src attribute of script tag is used to add or include external JavaScript file into any number of html files. It increases the code re usability. <script type="text/javascript" src="external.js"></script> First create a JavaScript file and then save the file with .js extension.

How do I install yarn and Node JS on rails?

If you are using a JavaScript bundler in your Rails application, Node.js and Yarn must be installed. Find the installation instructions at the Node.js website and verify it’s installed correctly with the following command: The version of your Node.js runtime should be printed out. Make sure it’s greater than 8.16.0.

How do I use import maps in a Rails application?

Import maps are the default for new Rails applications, but if you prefer traditional JavaScript bundling, you can create new Rails applications with your choice of esbuild, webpack, or rollup.js. To use a bundler instead of import maps in a new Rails application, pass the —javascript or -j option to rails new:


1 Answers

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));
like image 114
Jonathan Allard Avatar answered Oct 13 '22 04:10

Jonathan Allard