I put blocks which use jQuery in the body of one (and only one) cshtml file which uses the template and they causes error because jQuery is not loade
Just enclose it inside section scripts
in .cshtml Page as shown.
@section scripts{
<script>
$(document).ready(function () {
// .....
});
</script>
}
You can use sections :
in your layout :
...
<script src="/Scripts/jquery-1.10.2.js"></script>
<script src="/Scripts/bootstrap.js"></script>
@RenderSection("scripts", required: false)
...
in yours cshtml :
@section scripts {
<script>
$(document).ready(function () { /// $ not defined.
// .....
});
</script>
}
It's a best practice to load your javascript files after every possible HTML element.
Knowing this, I would place your document ready handler after loading all libs.
<hr />
<footer>
</footer>
</div>
<script src="/Scripts/jquery-1.10.2.js"></script>
<script src="/Scripts/bootstrap.js"></script>
<script>
$(document).ready(function () { /// $ not defined.
// .....
});
</script>
If the script is at the top of the page, and there are problems then it may cause the page to stop/take a long time loading. Putting them at the bottom allows the page the render fully before your scripting goes to work.