locked
Calling a script from another file doesn't work. MVC @Scripts.Render and @RenderSection ....(solution) RRS feed

  • Question

  • User1277400347 posted

     Today I spent a bunch of time trying to find out why a view with this script couldn't find the DoThis() method.

     

     <script type="text/javascript">
         $(document).ready(
             function () {
                 alert("In the function about to call main.DoThis()");
                 DoThis();
             });
     </script>

     Step 1) I used a layout for this view that had this render statement for a bundle we created.

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

    The function DoThis was in a file named main.js contained in that bundle. 

    Step 2) I attempted to debug using Chrome's F12 function setting breakpoint on the script above and it kept saying that the script wasn't found when DoThis() was executed.

    Step 3) I looked in the scripts folder from Chrome and the script I wanted was there...#?@?$

    Being relatively new to Javascript and how browsers load it.... I never once figured the reason it wasn't found was because there were errors in the Javascript itself.  The only indicator I had was a small red mark at the end of the JS file that indicated I hadn't included a "}"...    Wow!  Good lesson I guess.

    If you have this symptom better take a close look at the Javascript.

    Tuesday, September 23, 2014 4:56 PM

All replies

  • User197322208 posted

    if you look at the console, sure you will find a javascript error

    Tuesday, September 23, 2014 5:15 PM
  • User1277400347 posted

    Yes and thanks for pointing that out.  The editor also showed the issue but it was not a compiler error, rather it was a small red tick mark at the very bottom of the JavaScript page which was out of view from where I was working.  

    Thursday, September 25, 2014 9:19 AM
  • User197322208 posted

    I enabled in IE to display all javascript errors.

    Thursday, September 25, 2014 7:55 PM