jQuery ready() in asp.net mvc2 only fires on first visit
<script type="text/javascript">
$(document).ready(function() {
alert("Hello jQuery.开发者_Python百科");
});
</script>
This works the first time I request /Home/Index
, but if I navigate to eg /Account/Login
then back to /Home/Index
it doesn't work. Doing the same thing using a webforms project works every time. What am I missing? Thanks.
this is not the answer. try this:
<div id="test-ready"></div>
<script type="text/javascript">
$("#test-ready").append("<span>pre ready</span><br />");
$(document).ready(function() {
$("#test-ready").append("<span>in ready (hello jQuery)</span><br />");
//alert("Hello jQuery.");
});
$("#test-ready").append("<span>pos ready</span><br />");
</script>
Try this instead:
$(function() {
alert("Hello jQuery.");
});
http://api.jquery.com/jQuery/#jQuery3
Maybe it will give you less trouble.
Thanks for the replies. The answer, and a question: The relative reference to the .js files was OK for http: //localhost:12345/
but not http: //localhost:12345/Home/Index
- effectively they're the same resource, but not internally for mvc, despite the fact the mvc "paths" are logical not physical - so why does mvc make .js references relative to a logical path?
精彩评论