﻿# 如何透過 Visual Studio 對任意網站的 JavaScript 除錯

在 Visual Studio 2005/2008 的\[工具\]選單中有個 Attach to Process 功能，可以將目前正在執行中的程序「附加」到 Visual Studio 的除錯工具(Debugger)中，讓 Visual Studio 隨時可以對任意程序進行除錯工作。這是個很方便的功能，你甚至可以不用先開啟方案檔或專案，就可以直接透過 Attach to Process 功能進行除錯，當然這也包括針對 Internet Explorer 進行除錯。

 ![Visual Studio / Tools / Attach to Process](https://stwillblogassets.blob.core.windows.net/files/images/external/stwillblogassets.blob.core.windows.net/22d209bd3b5d8752eb5e-image_8.png)

當開啟 Attach to Process 後，會讓你選擇目前正在執行的程序(Process)，如下圖示，我的 IE 開了一個網站到 [http://www.miniasp.com/](http://www.miniasp.com/)，在 Available Process 清單中的 Title 欄位很容易可以辨認出哪一個行程(Process)是你要的，選擇確定後按下右下角的 Attach 按鈕，就會進入除錯模式了。當 IE 被附加(Attach)上去後，若有任意網站的 JavaScript 出現錯誤，Visual Studio 就會直接將程序暫停，並進入中斷點，讓你仔細的看看哪裡出問題了，我好幾次都是透過這個方式協助我解決已上線網站的細微錯誤。

[![Visual Studio - Attach to Process](https://stwillblogassets.blob.core.windows.net/files/images/external/stwillblogassets.blob.core.windows.net/4381e78bc75f2dfafdbe-image_thumb.png)](https://stwillblogassets.blob.core.windows.net/files/images/external/stwillblogassets.blob.core.windows.net/eeff65f5593423de8111-image_2.png)

不過，如果要能正確除錯 JavaScript，還必須檢查你「網際網路選項」中的「進階」設定，其中的「停用指令碼除錯 (Internet Explorer)」與「停用指令碼除錯 (其他)」不能被勾選。

 ![「網際網路選項」中的「進階」設定](https://stwillblogassets.blob.core.windows.net/files/images/external/stwillblogassets.blob.core.windows.net/ec89ed72e7e6ac4f36ef-image_14.png)

**相關連結**

-   [DEBUG ANY JAVASCRIPT IN YOUR BROWSER WITH VISUAL STUDIO](https://web.archive.org/web/20210305042505/http://blog.tylerholmes.com/2008/09/debug-any-javascript-in-your-browser.html)
