Why does $ (window) .load () work, but not $ (document) .ready ()?

I am working with a rails 3 application and I want to use a sorted list. I work with the method shown here . My application uses jQuery, and there the js file is included in my application layout, which calls $(document).ready()to configure some visual materials. It seems to be working fine.

However, when I try to call $(document).ready()through my template through content_for :javascriptto configure a sortable list, this code never works. I have the required yield :javascriptcall in my layout file, and if I load the page and look at the source, everything looks fine. The code never runs, i.e. This instance $(document).ready()never fails.

I just found that if I replaced $(document).ready()with $(window).load(), then my js code will run.

So my question is: Why does it $(document).ready()crash and $(window).load()work?

the code

It works:

<% content_for :javascript do %>

<script>
   $(window).load(function(){
     alert('it works!');
   });
</script>
<% end %>

This does not work

<% content_for :javascript do %>

<script>
   $(document).ready(function(){
            alert('it works!');
   });
</script>
<% end %>

Here layout

<!DOCTYPE html>
<html>
  <head>
    <title><%= content_for?(:title) ? yield(:title) : "Untitled" %></title>

    <!-- Reset Stylesheet -->
      <%= stylesheet_link_tag "reset" %>
        <!-- Main Stylesheet -->
      <%= stylesheet_link_tag "style" %>
        <!-- Invalid Stylesheet -->
      <%= stylesheet_link_tag "invalid" %>

        <%= javascript_include_tag :defaults, "nested_form", "DD_belatedPNG_0.0.7a", "simpla.jquery.configuration", "facebox", "jquery-ui.min" %>
        <%= yield :javascript %>

    <%= yield(:head) %>
    <%= csrf_meta_tag %>

  </head>
  <body onload="initialize()"><div id="body-wrapper">
+5
source share
2 answers

There is a conflict with the registered body callback onready=. See jquery docs. You must delete <body onload="initialize()">.

http://api.jquery.com/ready/

It is also possible that the resource is loading very VERY slowly. However, more likely.

.ready . , , . Firebug, , .

+2

... , SCRIPT BODY , DOM?

<body>
  <!-- all your HTML here -->
  <script>
    //your starting Javascript code here
  </script>
</body>
+1

All Articles