Javascript document element in rails layout file -


i'm working on web application using rails. have individual html.erb files run script.js on each file. want follow dry want put script.js in layout's file instead of each individual view file.

here script.js:

if(document.documentelement.clientwidth < 570) {   //do stuff } 

this works fine when put in each of view individually doesn't work when put in layouts file. have feeling because using document. if true i'm not sure should replace document with. if not true i'm not sure wrong.

if me understand why script.js doesn't work when put in layouts file appreciated.

what can do:

put files in:

lib/assets/javascript or put file in vendors/assets/javascript

after load file in application.js //=require script.js

in script.js, can add conditions if want run script few pages. can check example if div exists html page

if ($('#mydiv').length){   //do } 

if using turbolinks, situation change, need more things: can install jquery-turbolinks , have methods in function

$(document).ready(function () { /* ... */ }); 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -

jquery - Keeping Kendo Datepicker in min/max range -