views:

37

answers:

3

Hi all,

<script type="text/javascript">
        $(document).ready(function() {
            alert("Hello jQuery.");
        });
</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 everytime. What am I missing? Thanks.

A: 

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>
andres descalzo
A: 

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?

A: 

Try this instead:

$(function() {
    alert("Hello jQuery.");
});

http://api.jquery.com/jQuery/#jQuery3

Maybe it will give you less trouble.

ntcolonel
that is the same thing really, just another way of saying it.
Mark Schultheiss
Yeah, but sometimes just changing the way you say the same thing helps. That's why it figured a "maybe".
ntcolonel