Debug IE中的JavaScript
默认的script debugging是被关闭的, 要开启需要:
Tools->Internet Options…->Advanced->Disable Script Debugging
在XP SP2上, 选项被拆分成了两项:
Tools->Internet Options…->Advanced->Disable Script Debugging (Internet Explorer)
Tools->Internet Options…->Advanced->Disable Script Debugging (Other)
当你开启了Script Debugging之后, 菜单项‘View->Script Debugger’会变的可见, 使用它你就可以断入debugger中了.
一般使用的debugger有三种, 第一个是Visual Studio, 第二个是Microsoft Script Debugger, 第三个是Microsoft Script Editor.
简单说一下Visual Studio中的script debugging.
第一种方法: 比如说有一个包含JavaScript的html的页面.
- Enable script debugging in IE (see image: Tools -> Internet Options -> Advanced)
- Open the html file in Visual Studio
- Set a breakpoint and hit F5 / Start Debugging or hit F10 / F11 to single step
剩下的就跟在debugging一个普通程序一样了.
第二种方法: 如果应用程序不可能通过在visual Studio中点击F5来启动, 比如说开始debug之前脚本还在服务器上, 那么就需要用第二种方式了.
- Open the web site in Visual Studio and open the .js file for editing
- Set a breakpoint at a suitable place in the .js file (eg in the this.initialize function)
- In Visual Studio go to Debug -> Attach to Process
- Ensure that either Automatic or Script is selected as the type of code to debug (see images below)
- Select the relevant IE process from the available process and click "Attach" (see image below)
- Return to IE and load up the gadet (or reload it) and the breakpoint should fire and you'll drop back into Visual Studio as before
唯一不爽的地方就是你需要经常在"Attach to Process"对话框中进进出出. 相比起Visual Studio的强大功能来说, 这个缺点也算不得什么.
如果你希望在Visual Studio中新开一个浏览器, 你可以在solution Explorer中选择一个页面, 右键单击, 选择"Browse With". 这里你还能更改默认的浏览器.
原文:
Script Debugging with Visual Studio
http://blogs.msdn.com/mikeormond/archive/2006/01/13/512496.aspx