0

I have a main view that render another partial view as follow:-

@Html.Partial("_PagedTable",Model)

I have define Scripts inside the _PagedTablepartial view as follow:-

<div id ="RackTable">
    <script type="text/javascript">
        $(document).ready(function () {

Currently the scripts inside the partial view is working well , but when i opened the F12 developer tool on IE and the firebug tool, there will always be the following error on the scripts , raised on the scripts that are defined inside the partial view:-

SCRIPT5009: '$' is undefined

Altohugh there is an error but users will not notice this error , since no error messages will be displayed (of course unless they open the F12 developer tool), and the scripts inside the partial view is working well. so what does this error indicates exactly ?

John John
  • 1
  • 72
  • 238
  • 501
  • This error indicates that jQuery has not been loaded properly. this also means that your jQuery shouldn't be working. – Jay Blanchard May 07 '14 at 13:30
  • but it is working well , as i have indicated, and also no error will be displayed to the users .. – John John May 07 '14 at 13:32
  • Hmmmm...then that would be unusual. This indicates that you're calling a function somewhere whose methods have not been defined. – Jay Blanchard May 07 '14 at 13:33
  • i am 100% sure that the Scripts is working well, as when i removed the Scripts , I lost all the Jquery functionalities .. so this means that the Scripts is loaded and is working well .. – John John May 07 '14 at 13:39

2 Answers2

0

Are you sure the code within

$(document).ready(function () {

is being executed?

the error means jQuery not loaded, hence your script would not be able to execute

I have found similar problem from SO (such as IE10 and jQuery: SCRIPT5009: "$" is undefined), but they all seem to happen on IE but you mention it happen on firefox as well, so I would only suggest check if your jQuery is loaded properly or not.

Community
  • 1
  • 1
Alan Tsai
  • 2,465
  • 1
  • 13
  • 16
  • yes it is working well, since if i remove the Jquery which is raising the error , all the JQuery functionalities will stop working , this indicates that the Jquery is working and loading well... – John John May 07 '14 at 13:37
  • another question why i am receiving this error. as i have added all the Jquery scripts inside my layout view ? is this related to the fact that these Scripts are defined directly insdie my partial view ? – John John May 07 '14 at 13:38
  • in this case, can you try take out other script you are using besides jquery, just see if it can narrow down the problem. BTW, sorry was too late to see your comment above – Alan Tsai May 07 '14 at 13:44
0

Commonly, jQuery is referenced in your Layout page (MasterPage) only in the end of the file. So, your partial view is coming before this, when jQuery wasn't loaded yet.

What is strange is the browser throws this exception, but jQuery still works.

Worth trying using Razor Sections:

<html>
<body>

    [View]
    [_PartialView]
        @section Scripts {
            <script type="text/javascript">
                $(document).ready(function () {
                    // stuff
                });
            </script>
        }
    [_PartialView - END]

    <!-- jQuery and other references -->
    <script src="jquery.min.js"></script>

    @RenderSection("Scripts", required: false)
</body>
</html>
Andre Figueiredo
  • 12,930
  • 8
  • 48
  • 74