Execute javascript everytime a route is hit in rails -
this question has answer here:
when page loads first time code in /assets/javascript/home.js executes, positions navbar using bootstraps affix.
$('.navbar').affix({ offset: { top: function() { return this.bottom = $('.hero').outerheight(true); } } });
when go route such /blog
, don't want code execute , want static css render works great. problem when got home route(/
), javascript doesn't execute again , navbar using natural css. if refresh @ point, page reload , navbar positioned correctly.
i think problem because of turbo-links not experienced enough rails resolve issue. hope can shed light!
if you're using turbolinks, can this:
$(function() { if (window.location.pathname === '/') { // run code } });
turbolinks changes $(fn) (which shortcut $(document).ready ) run after changing other path, it'll run code on each path change.
Comments
Post a Comment