Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy does $(document).ready() never fire but $(window).load() will?
    primarykey
    data
    text
    <p>I'm working with Rails 3.2.8 and am running jQuery v1.8.2. </p> <p>The following works:</p> <pre><code>$(window).load(function(){ alert('window load is working'); }); </code></pre> <p>But this does not work:</p> <pre><code>$(document).ready(function() { alert('document ready is working'); }); </code></pre> <p>In fact, whenever I switch <code>$(document).ready()</code> with <code>$(window).load()</code> for any and all jQuery functions I am testing, they are fired. These include accordions, draggable elements... </p> <p>Here is my <strong>application.html.erb</strong> file:</p> <pre><code>&lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;&lt;%= full_title(yield(:title)) %&gt;&lt;/title&gt; &lt;%= stylesheet_link_tag "application", :media =&gt; "all" %&gt; &lt;%= javascript_include_tag "application" %&gt; &lt;%= csrf_meta_tags %&gt; &lt;/head&gt; &lt;body background="&lt;%= image_path(random_bg) %&gt;"&gt; &lt;%= render 'layouts/header' %&gt; &lt;div id="container"&gt; &lt;% flash.each do |key, value| %&gt; &lt;div class="alert alert-&lt;%= key %&gt;"&gt;&lt;%= value %&gt;&lt;/div&gt; &lt;% end %&gt; &lt;%= yield %&gt; &lt;/div&gt; &lt;%= render 'layouts/footer' %&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>The resulting html...</p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;title&gt;Derp&lt;/title&gt; *bunch of css files* &lt;script src="/assets/jquery.js?body=1" type="text/javascript"&gt;&lt;/script&gt; &lt;script src="/assets/jquery_ujs.js?body=1" type="text/javascript"&gt;&lt;/script&gt; &lt;script src="/assets/jquery-ui.js?body=1" type="text/javascript"&gt;&lt;/script&gt; &lt;script src="/assets/collections.js?body=1" type="text/javascript"&gt;&lt;/script&gt; &lt;script src="/assets/global.js?body=1" type="text/javascript"&gt;&lt;/script&gt; &lt;script src="/assets/derp.js?body=1" type="text/javascript"&gt;&lt;/script&gt; &lt;script src="/assets/sessions.js?body=1" type="text/javascript"&gt;&lt;/script&gt; &lt;script src="/assets/static_pages.js?body=1" type="text/javascript"&gt;&lt;/script&gt; &lt;script src="/assets/test.js?body=1" type="text/javascript"&gt;&lt;/script&gt; &lt;script src="/assets/users.js?body=1" type="text/javascript"&gt;&lt;/script&gt; &lt;script src="/assets/application.js?body=1" type="text/javascript"&gt;&lt;/script&gt; .... </code></pre> <p>I have also gone throw stackoverflow, searched the web neurotically, and tried all the solutions I could find:</p> <ul> <li>I have tried disabling local jQuery and using Google to host jQuery</li> <li>I have tried playing around with the placement of <code>javascript_include_tag</code> (moving it above stylesheets, moving it before )</li> <li>I tried including <code>$(document).ready()</code> inside <code>$(window).load()</code></li> </ul> <p>But <code>$(document).ready()</code> still does not fire... There are no problems in console (checked on Chrome, Safari, Firefox)</p> <p>A similar question was asked <a href="https://stackoverflow.com/questions/6022357/why-does-window-load-work-but-not-document-ready">here</a>, but he never received an answer to his main question.</p> <p><strong>Why does jQuery only work when I replace <code>$(document).ready()</code> with <code>$(window).load()</code>?</strong></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload