I'm working with a rails 3 app, and I want to use a sortable list. I'm working with the method shown here. My app uses JQuery, and there's a js file included in my app's layout that calls $(document).ready()
to set up some visual stuff. That seems to be working fine.
However, when I attempt to call $(document).ready()
in my view template via content_for :javascript
to set up the sortable list, that code never fires. I do have the requisite yield :javascript
call in my layout file, and if I load the page and look at the source everything looks fine. The code never runs, though – i.e. this instance of $(document).ready()
never fires.
I've just found that if I replace $(document).ready()
with $(window).load()
then my js code runs.
So my question is: Why would $(document).ready()
fail and $(window).load()
work?
Code
This works:
<% content_for :javascript do %>
<script>
$(window).load(function(){
alert('it works!');
});
</script>
<% end %>
This doesn't work
<% content_for :javascript do %>
<script>
$(document).ready(function(){
alert('it works!');
});
</script>
<% end %>
Here's the 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">
…
I'm working with a rails 3 app, and I want to use a sortable list. I'm working with the method shown here. My app uses JQuery, and there's a js file included in my app's layout that calls $(document).ready()
to set up some visual stuff. That seems to be working fine.
However, when I attempt to call $(document).ready()
in my view template via content_for :javascript
to set up the sortable list, that code never fires. I do have the requisite yield :javascript
call in my layout file, and if I load the page and look at the source everything looks fine. The code never runs, though – i.e. this instance of $(document).ready()
never fires.
I've just found that if I replace $(document).ready()
with $(window).load()
then my js code runs.
So my question is: Why would $(document).ready()
fail and $(window).load()
work?
Code
This works:
<% content_for :javascript do %>
<script>
$(window).load(function(){
alert('it works!');
});
</script>
<% end %>
This doesn't work
<% content_for :javascript do %>
<script>
$(document).ready(function(){
alert('it works!');
});
</script>
<% end %>
Here's the 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">
…
Share
edited May 16, 2011 at 23:06
CharlieMezak
asked May 16, 2011 at 19:31
CharlieMezakCharlieMezak
5,9991 gold badge40 silver badges54 bronze badges
6
- 1 post some example code and i'll give you a cupcake. – pixelbobby Commented May 16, 2011 at 19:35
- I don't think any one can answer that without some example code or more details. – mbxtr Commented May 16, 2011 at 19:42
- OK, sorry. I'll post some code…it's so simple that I'm not sure what to post. Wherever the problem is, I don't think it's in the way that I'm calling these functions or including them in the rails files. – CharlieMezak Commented May 16, 2011 at 19:45
-
Can you post your template showing exactly where your
yield
call is? – Dylan Markow Commented May 16, 2011 at 21:04 -
Are you using Safari to test? There are some edge cases where
document.ready
doesn't work properly with Safari. Example, if you need to manipulate images. Anyway, it is hard to tell becuase your js is affecting#text_items
but you don't show that area of your html. – Kevin Peno Commented May 16, 2011 at 22:37
2 Answers
Reset to default 2There is a conflict with the body's onready=
callback registered. See the jquery docs. You must remove the <body onload="initialize()">
.
http://api.jquery./ready/
Also a possibility is a resource being loaded very VERY slowly. However the first is more likely.
.ready
is triggered when resources are loaded. It is possible a resource may be perpetually in a wait state because the server never closed the socket when sending the resource to the browser. Use firebug's network monitor tool to track down if this is the case.
What about a workaround... Did you know that a SCRIPT
tag at the end of the BODY
tag, is executed when everything before is loaded in the DOM
?
<body>
<!-- all your HTML here -->
<script>
//your starting Javascript code here
</script>
</body>
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744918023a4600957.html
评论列表(0条)