Debug a script that sits in a partial view

Debug a script that sits in a partial view

回答1

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:

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

 

回答2

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:

//# sourceURL=_fooPartialView.cshtml

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

 

回答3

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:

Linking JavaScript Libraries in User Controls

and

Include JavaScript file in partial views

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

 

我的经验是,

需要回答2和回答1配合起来用才可以。

 

 

posted @   ChuckLu  阅读(35)  评论(0编辑  收藏  举报
编辑推荐:
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· C# 深度学习:对抗生成网络(GAN)训练头像生成模型
· .NET 适配 HarmonyOS 进展
阅读排行:
· 用 DeepSeek 给对象做个网站,她一定感动坏了
· DeepSeek+PageAssist实现本地大模型联网
· 手把手教你更优雅的享受 DeepSeek
· 腾讯元宝接入 DeepSeek R1 模型,支持深度思考 + 联网搜索,好用不卡机!
· 从 14 秒到 1 秒:MySQL DDL 性能优化实战
历史上的今天:
2017-11-09 chrome的F12的inspect使用
2017-11-09 Macro expressions in kentico
2017-11-09 kentico version history and upgrade
2017-11-09 Compiler Warning (level 2) CS0436
2017-11-09 Creating new web parts kentico 10
2017-11-09 创建一个web user control
2017-11-09 kentico api
点击右上角即可分享
微信分享提示