I’m working with Rails 3.2.8 and am running 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() with $(window).load() for any and all jQuery functions I am testing, they are fired. These include accordions, draggable elements…
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>
The resulting 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 have also gone throw stackoverflow, searched the web neurotically, and tried all the solutions I could find:
- I have tried disabling local jQuery and using Google to host jQuery
- I have tried playing around with the placement of
javascript_include_tag(moving it above stylesheets, moving it before ) - I tried including
$(document).ready()inside$(window).load()
But $(document).ready() still does not fire… There are no problems in console (checked 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 I replace $(document).ready() with $(window).load()?
Sorry guys, I solved it by removing some crappy javascript I wrote. It was supposed to be a code that would display labels in browsers that don’t support placeholders. Somehow as soon as I deleted the file everything resumed working like normal… Weird how I always spend hours trying to fix a problem and seem to solve them moments after asking a question here!