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>
<%= stylesheet_link_tag "reset" %>
<%= stylesheet_link_tag "style" %>
<%= 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">
…
source
share