CoffeeScript code only works after press home menu <p>I am using Rails 5. I have a <code>home_controller</code> with index action and <code>root 'home#index'</code>. There's a button on index.html.erb, i want to press this button call some js code (pop window).</p> <p>But my coffee script code only was call after i press <strong>Home</strong> menu. So i am wondering <code>/</code> and <code>home#index</code> are different? Thank you for your help.</p> <p><strong>application.html.erb</strong></p> <pre><code>&lt;nav&gt; &lt;li&gt;&lt;%= link_to "Home", "/"%&gt;&lt;/li&gt; &lt;/nav&gt; </code></pre> <p><strong>index.html.erb</strong></p> <pre><code>&lt;button id="myBtn"&gt;Open Modal&lt;/button&gt; &lt;div id="myModal" class="modal"&gt; &lt;div class="modal-content"&gt; &lt;span class="close"&gt;×&lt;/span&gt; &lt;p&gt;Some text in the Modal..&lt;/p&gt; &lt;/div&gt; &lt;/div&gt; &lt;%= render :partial =&gt; 'stories/stories' %&gt; </code></pre> <p><strong>home.coffee</strong></p> <pre><code>modal = document.getElementById('myModal'); btn = document.getElementById("myBtn"); span = document.getElementsByClassName("close")[0]; btn.onclick = -&gt; modal.style.display = "block" span.onclick = -&gt; modal.style.display = "none" window.onclick = (event) -&gt; modal.style.display = "none" if event.target == modal </code></pre>