Question

I've editing this original question as I think I've narrowed down the problem...

I have one view in my site that will not let me put $document.ready within a masterpage contentplaceholder. I've stripped this page to the bare bones and the only thing that is special about it is it has a custom route in global.asax

 routes.MapRoute("Books",
                 "{controller}/{action}/{keywords}/{pageNumber}",
                  new { controller = "Books", action = "SearchResults" }
                 );

Any idea why this custom route would stop $document.ready working correctly when put in a masterpages contentplaceholder zone?

Was it helpful?

Solution

I had the same problem and it turned out that when I used a certain route it changed the perceived file hierarchy of the site such as the ../../Content link for the .js file didn't work any more. I fixed it by changing my jquery script reference to look like this:

<script src="<%= Url.Content("~/Content/jquery-1.2.6.min.js") %>" type="text/javascript"></script>

OTHER TIPS

Your master page (or view page if you're not using master pages) needs to reference jquery. This is included in the latest Beta release of the MVC framework.

Check to make sure you have jQuery included in the tag of your page.

check the syntax as well...

$(document).ready(function() { alert('loaded'); });

these shortened versions also work:

$().ready(function() { alert('loaded'); });
$(function() { alert('loaded'); });

Just stick it somewhere within the content control of your view page in a <script ...> tag.

<asp:Content ID="Content1" ContentPlaceHolderID="MainContentPlaceHolder" runat="server">
    <div class="contentItem">
        <%!-- yadda --%>
    </div>

    <script type="text/javascript">
        $(document).ready(function() {
         // do your worst
        });
    </script>
</asp:Content>

If you have stuff that runs on every page, you can peel that off into a .js file and access it from the master page. But for functions relating to a specific view, this is probably the simplest way to go and easiest to maintain.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top