Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails Include Third Party Javascript Library File

I've got a Javascript library for AJAX file uploading that I need to include on only one page. Where is the best folder for this for file? app/assets/javascripts? vendor/assets/javascripts? lib/assets/javascripts? And then I need to be able to include it on only one page. Or should I just add it to application.js and have it included on every page (even know I'm only using it on one page?)

I figured for performance my best bet would be to put the minified JS file somewhere, and just include it with a javascript_include_tag on the page I need it, by using yield(:head) and content_for(:head)? Thank you.

like image 890
b0xxed1n Avatar asked Jan 27 '14 03:01

b0xxed1n


People also ask

Where do I put JavaScript library?

Installing the library. Once you have the . js file, you need to put it in the same place as the rest of your project. For simplicity's sake, put it in the same folder that your sketch.

What are third party libraries in JavaScript?

Third-party JavaScript libraries can be a quick and effective way to fill the gap in any JS-powered application. NetSuite Commerce sites include a number of these libraries (such as jQuery, Bootstrap and Underscore already, and it is not unreasonable to want to include your own third-party libraries.

What is an external JavaScript library?

External JavaScript is when the JavaScript Code(script) is written in another file having an extension . js and then we link this file inside the <head> or<body> tag of our HTML file in which the code is to be added.


1 Answers

Third party JavaScripts should go inside vendor/assets/javascripts directory, because app/assets/javscripts is for your application specific JavaScripts and lib/assets/javascripts is for your libraries code which is separate to your application or libraries that you have shared across different applications.

Please reference Asset Organization for further details.

As far as inclusion of javascripts in your application/page if you're using it only on a specific page then including the file with javascript_include_tag is the approach I would take as well.

like image 99
vee Avatar answered Nov 07 '22 19:11

vee