2

I'm building an ASP.NET MVC3 website. I have some javascript in my .cshtml file:

<div>hello!</div>

<script type="text/javascript">
   $(document).ready(function () { alert("ready!"); })
</script>

Google Chrome's built-in debugger doesn't see this javascript snippet, so I can't debug it.

How can I set a breakpoint in this javascript snippet in Google Chrome's built-in debugger?

Judah Gabriel Himango
  • 58,906
  • 38
  • 158
  • 212
  • break the function onto multiple lines. Then, you can set a breakpoint on the `alert` line. – Joseph Yaduvanshi Jul 07 '11 at 22:03
  • Also, as a side note, if this is located within an `@Html.BeginForm` code block in Razor view engine, you may have to change how you're creating that block (curly braces may break compilation of your view). – Joseph Yaduvanshi Jul 07 '11 at 22:07
  • 2
    @Jim: No, you won't. Razor only looks for the closing `}` outside any HTML tags (in code context). – SLaks Jul 07 '11 at 22:13
  • @SLaks: That's not true. I've specifically run into this issue with Razor's inability to parse multiple nested blocks inside of a `@using(Html.BeginForm())` block. This was just after the official release of MVC3, it may have changed. – Joseph Yaduvanshi Jul 07 '11 at 22:18
  • 1
    @Jim: I'm pretty sure you're wrong. Can you show me an example of troublesome markup? – SLaks Jul 07 '11 at 22:20
  • @Slaks: Sorry, man. It's code for one of the largest companies in the world. I'm not going to mess with that. But, I can assure you, changing it from the disposable to two separate calls as in the answer I posted fixed the problem on multiple views. Someone on my team submitted the issue to Microsoft and never heard back. – Joseph Yaduvanshi Jul 07 '11 at 22:24
  • Nope, has nothing to do with multiple lines. (That was just a sample. My real code, with multiple lines, can't be debugged in Chrome because it's a javascript snippet, part of my .cshtml – Judah Gabriel Himango Jul 07 '11 at 22:39

2 Answers2

1

Weird, works great for me:

  1. Go to the Scripts tab
  2. You will see a dropdown list of all referenced javascript files, select the one that corresponds to the inline scripts (:9038 in my screenshot below)
  3. Set your breakpoint

enter image description here

You might also consider FireBug as an alternative.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
0

In MVC3, running the razor view engine, I've had braces really mess up the parsing of the view.

For instance, if you have:

@using(Html.BeginForm()){
<div>hello!</div>

<script type="text/javascript">
   $(document).ready(function () { alert("ready!"); });
</script>
}

You may have issues with the script's braces. Try changing it to:

  @{ Html.BeginForm(); }
    <div>hello!</div>

    <script type="text/javascript">
       $(document).ready(function () { 
            alert("ready!"); 
       });
    </script>
   @{ Html.EndForm(); }

This may or may not be the answer to your question, but it took me forever to figure out what was wrong with a few of my forms. I wasn't embedding scripts in them, though.. it was blocks for conditional logic that would break everything for me.

EDIT After a little more research, I found an issue someone had which led me to the solution: aspnet.codeplex.com/workitem/7551.

My commit message (from a codebase I don't physically have access to any longer) suggests it may have been bad markup. The dev who wrote the offending pages liked to use capitalized tags, even though we were using XHTML 1.1 doctype. He also had a lot of attributes being conditionally compiled and/or populated. For example:

<div class="something @myHelper(someFlag)"></div>
<div @{ isSomeFlag ? <text>class="asdf"</text> : "" }></div>

My solution should be considered a temporary fix.

Joseph Yaduvanshi
  • 20,241
  • 5
  • 61
  • 69
  • Your first example causes no trouble for me with Razor, and I personally have never experienced any issues with the Razor parser with respect to JS vs. C# braces. – Kirk Woll Jul 07 '11 at 22:23
  • @Kirk: I've used the first example a number of times myself. This may be an edge case, but I did encounter it three or so times. All of which were someone else's code, of course. :P – Joseph Yaduvanshi Jul 07 '11 at 22:25
  • @Kirk: Also, you're probably like me and avoid using too much logic in a view. That was very common on these pages, ~75-100 lines of control structures. – Joseph Yaduvanshi Jul 07 '11 at 22:28
  • Code and markup are mutually exclusive; there is no situation in which Razor can expect both markup and `}`. – SLaks Jul 07 '11 at 22:29
  • @SLaks: I would tweak the code and post an example if I had access to the codebase still, but I recently moved and left that position. I'll reach out to someone who is still on the team and see if I can get a real example. – Joseph Yaduvanshi Jul 07 '11 at 22:32
  • Nope, sorry, the problem isn't curly braces. It's that script inside .cshtml doesn't even show up in Chrome's debugger. – Judah Gabriel Himango Jul 07 '11 at 22:40
  • @Judah: Did you try wrapping the script tags in `` tags? – Joseph Yaduvanshi Jul 07 '11 at 22:42
  • @SLaks: I wasn't able to get the code from my old coworker. I found an issue someone had which led me to the solution: http://aspnet.codeplex.com/workitem/7551, and my commit message suggests it may have been bad markup. The dev who wrote the offending pages liked to use capitalized tags, even though we were using XHTML 1.1 doctype. That *would* explain it, I guess. – Joseph Yaduvanshi Jul 07 '11 at 22:46
  • @SLaks: Do you have any idea why using the disposable form of the `Html.BeginForm` would be less forgiving of poorly-written HTML markup? – Joseph Yaduvanshi Jul 07 '11 at 22:48
  • @SLaks: I also updated my answer with some examples of the wackiness I encountered regularly (and on the offending pages). – Joseph Yaduvanshi Jul 07 '11 at 23:00
  • @Jim: Tags, including ``, are statements, not expressions. You can't put them in conditionals. – SLaks Jul 07 '11 at 23:01
  • @Slaks: the `` tags may or may not have been there. It's been a couple months since I last saw that code. But, there was a lot of weirdness like that, and it made me want to bang my head on my desk every time I saw it. Thanks for asking more about, sorry I couldn't provide the actual code. – Joseph Yaduvanshi Jul 07 '11 at 23:24