I have javascript that will be run onload in my application.js, but can I isolate it to run on a certain page only without putting it inline on the page itself? I don't want it to run on every page if possible.
Thanks!
Asked
Active
Viewed 64 times
0

perseverance
- 6,372
- 12
- 49
- 68
1 Answers
1
Basically the two ways to do it are:
1.) Using content_for to render specific js based on the page you are in. This will not be rendered inline but will instead be rendered with the rest of your js as you want. For more info on this check out Best way to add page specific javascript in a Rails 3 app?
2.) Add a wrapper div with class or id or do this directly on the body. You can then scope all your js correctly. ie
$(document).ready(function() {
if ($('body').hasClass('my_page')) {
//do the stuff you want
}
});