javascript 调试位于局部视图中的脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6901200/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 22:16:58  来源:igfitidea点击:

Debug a script that sits in a partial view

javascriptasp.net-mvc-3

提问by iLemming

Why I can't debug scripts that reside in a partial view, that gets created in runtime? To see the script in the list of scripts (in Chrome for example) and debug it, I have to move it to the "regular" view on the upper level or I have to move it to a separate .js file. But what, if the script so small that I don't want to move it anywhere, and still want to be able to debug it?

为什么我不能调试驻留在运行时创建的局部视图中的脚本?要查看脚本列表中的脚本(例如在 Chrome 中)并对其进行调试,我必须将其移至上层的“常规”视图,或者必须将其移至单独的 .js 文件。但是,如果脚本太小以至于我不想将它移动到任何地方,并且仍然希望能够调试它呢?

回答by John Kalberer

If you do not load the partial view via ajax (the view is in place at the initial page rendering) you can use 'debugger'. If the code you want to run is added to the dom IE will not know where the actual code is located that you want to debug. So:

如果您不通过 ajax 加载局部视图(视图在初始页面呈现时就位),您可以使用“调试器”。如果你想运行的代码被添加到dom IE 将不知道你想调试的实际代码在哪里。所以:

// javascript
var foo = 2;
debugger;
// more javascript

回答by omatase

There's a much better way to do this now, just use the syntax

现在有更好的方法可以做到这一点,只需使用语法

//@@ sourceURL=someValue

immediately after opening your script tag. Example:

打开脚本标签后立即。例子:

<script type="text/javascript">
    //@@ sourceURL=_fooPartialView.cshtml
    function foo() {}
</script>

--edit--

- 编辑 -

Apparently due to some IE compatibility issue javascript source mapping has been changed from the above to:

显然由于某些 IE 兼容性问题,javascript 源映射已从上述更改为:

//# sourceURL=_fooPartialView.cshtml

Also note, although not mentioned earlier, the @@ was only necessary for source mapping in razor views since "@" had other significance.

另请注意,虽然前面没有提到,@@ 仅在 razor 视图中的源映射中是必需的,因为“@”具有其他意义。

回答by Justin Beckwith

It's generally considered poor practice to include a script inside of a partial view. You could run into all kinds of issues with multiple script references and performance. The better approach here is to ensure the script gets moved up to a placeholder in your head tag. For a few examples on this, check out:

在局部视图中包含脚本通常被认为是不好的做法。您可能会遇到与多个脚本引用和性能有关的各种问题。这里更好的方法是确保脚本向上移动到您的 head 标签中的占位符。有关这方面的一些示例,请查看:

Linking JavaScript Libraries in User Controls

在用户控件中链接 JavaScript 库

and

Include JavaScript file in partial views

在部分视图中包含 JavaScript 文件

If you insist on loading the script from the partial, the 'debugger' approach above is very effective.

如果您坚持从部分加载脚本,上面的“调试器”方法非常有效。