Gerard
Gerard

Reputation: 2709

MVC 4.0 javascript doesn't work

Got a mobile application and want to run a javascript. Placed the script ("startstop") in a bundle:

    public static void RegisterBundles(BundleCollection bundles)
    {
        bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                    "~/Scripts/jquery-1.*"));

        bundles.Add(new ScriptBundle("~/bundles/startstop").Include(
                    "~/Scripts/startstop*"));
    ...

Included the bundle in the view:

@section Scripts {
    @Scripts.Render("~/bundles/startstop")    
}

Simplified the script:

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

When I ran the page/application, Action(s) from the startstop-script were not triggered/executed (IExporer/Chrome). However,When I copied the source of the page generated (including Scripts & Content) and opened the saved (copied) page in IExporer/ Chrome this startstop-script runs fine.

Anybody any idea what might be wrong? Thanks in advance for your help!

Upvotes: 0

Views: 964

Answers (2)

Gerard
Gerard

Reputation: 2709

Don't know why, but the problem is caused by @section.Scripts:

@section Scripts {
    @Scripts.Render("~/bundles/startstop")    
}

Without this section the startstop-script works fine:

@Scripts.Render("~/bundles/startstop")

does the job. The first case renders slightly different from the second case:

    </div>
  </div>
<script src="/Scripts/jquery-1.7.2.js"></script>
<script src="/Scripts/jquery.mobile-1.1.0.js"></script>
<script src="/Scripts/jquery-1.7.2.js"></script>
<script src="/Scripts/startstop.js"></script>
</body></html>

Whereas the working variant looks like:

     <script src="/Scripts/jquery-1.7.2.js"></script>
     <script src="/Scripts/startstop.js"></script>
     </div>
  </div>
<script src="/Scripts/jquery-1.7.2.js"></script>
<script src="/Scripts/jquery.mobile-1.1.0.js"></script>
</body></html>

Still puzzles me, but it works...!

Upvotes: 0

Dan Saltmer
Dan Saltmer

Reputation: 2165

If the script filename ends in .min.js, it get ignored. If this is the case, try adding this to the top of your RegisterBundles function.

bundles.IgnoreList.Clear();

Edit: clarified.

Upvotes: 1

Related Questions