20

I'm trying to run a jquery code which was put in my _Layout.cshtml as below:

...............
<script type='text/javascript'>
$(document).ready(function () {
   alert('Test');

});
</script>
  @Scripts.Render("~/bundles/jquery")
  @RenderSection("Scripts", required: false) 
</body>
</html>

The above code wasn't fired and when I inspected with Chrome Dev, it showed $ is not defined ( I can see all my jquery, jquery ui files are loaded )

It worked if I put this code in an external js file

I don't mind putting all my jquery code in external files, but really wanna clarify where I am doing wrong.

3
  • 1
    You are asking for jquery functions before you load the jquery library. Commented Aug 29, 2012 at 8:37
  • Ideally you should render scripts in the header part of your layout so any jquery on any of your pages will work. Commented Aug 29, 2012 at 17:26
  • Ideally all JavaScript should go into it's separate file and then it would have loaded just fine because your custom scripts are set to render in the layout right after jquery. Commented Feb 16, 2017 at 0:03

3 Answers 3

26

You need to introduce jquery before your script.

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

<script type='text/javascript'>
$(document).ready(function () {
   alert('Test');

});
</script>

  @RenderSection("Scripts", required: false) 
</body>
</html>
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, it worked. I used to put all my jquery libraries in header section, but according to my research recently, it's a good practice to put them before the closing body tag </body>
I'm confused. Most of what I read is you should always put jQuery script import at the end for better performance, which is exactly what ASP.NET MVC4 does by default. But I have never been successful in using jQuery this way, I always need to import jQuery at the start of the document in order for it to work. Is there something I am missing?
@AndreasLarsen: yes (you are missing something), microsoft markets themselves to management NOT to programmers, therefore when programmers adapt to subpar implementations, the problem is considered solved.
9

Andreas, i have the same issue.. for default the "_layout.cshtml" has "@Scripts.Render("~/bundles/jquery")" at the end of the document, but it doesn't work.. i cut it and paste it in the head and now it is working.

Comments

8

In order for jQuery to work, your script should come after <script src="/Scripts/jquery-2.0.3.js"></script>

In the _Layout.cshtml, just before the </body> tag you can find something like this

@Scripts.Render("~/bundles/jquery")
@RenderSection("scripts", required: false)

First you have the jQuery library. Then you have any scripts. That's what this means. So in your views do something like this.

@section scripts{
 //your script
}

This will make sure that all scripts comes under jQuery reference.

1 Comment

I forgot to wrap jQuery with @section scripts{} in my .cshtml view leading to the "jQuery not defined" error. It's a simple fix for a confusing error. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.