預設的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