I work with Rails 3.2.8 and run jQuery v1.8.2.
The following works:
$(window).load(function(){ alert('window load is working'); });
But this does not work:
$(document).ready(function() { alert('document ready is working'); });
In fact, whenever I switch $(document).ready() to $(window).load() for all and all the jQuery functions that I test, they start. These include accordions, draggable items ...
Here is my application.html.erb file:
<!DOCTYPE html> <html> <head> <title><%= full_title(yield(:title)) %></title> <%= stylesheet_link_tag "application", :media => "all" %> <%= javascript_include_tag "application" %> <%= csrf_meta_tags %> </head> <body background="<%= image_path(random_bg) %>"> <%= render 'layouts/header' %> <div id="container"> <% flash.each do |key, value| %> <div class="alert alert-<%= key %>"><%= value %></div> <% end %> <%= yield %> </div> <%= render 'layouts/footer' %> </body> </html>
Received html ...
<html> <head> <title>Derp</title> *bunch of css files* <script src="/assets/jquery.js?body=1" type="text/javascript"></script> <script src="/assets/jquery_ujs.js?body=1" type="text/javascript"></script> <script src="/assets/jquery-ui.js?body=1" type="text/javascript"></script> <script src="/assets/collections.js?body=1" type="text/javascript"></script> <script src="/assets/global.js?body=1" type="text/javascript"></script> <script src="/assets/derp.js?body=1" type="text/javascript"></script> <script src="/assets/sessions.js?body=1" type="text/javascript"></script> <script src="/assets/static_pages.js?body=1" type="text/javascript"></script> <script src="/assets/test.js?body=1" type="text/javascript"></script> <script src="/assets/users.js?body=1" type="text/javascript"></script> <script src="/assets/application.js?body=1" type="text/javascript"></script> ....
I also went into stackoverflow, searched for web neuroticism and tried all the solutions I could find:
- I tried disabling local jQuery and using google to host jQuery
- I tried playing around with the placement of
javascript_include_tag (moving it over the stylesheets, moving it earlier) - I tried to include
$(document).ready() inside $(window).load()
But $(document).ready() still doesn't work ... There are no problems in the console (tested on Chrome, Safari, Firefox)
A similar question was asked here , but he never received an answer to his main question.
Why does jQuery only work when replacing $(document).ready() with $(window).load() ?
source share