![](/img/trans.png)
[英]Javascript + Browser - How to detect when dev tools / firebug is open?
[英]When paused on a statement within the browser's dev tools, how to terminate execution immediately after that statement?
假設我有這個功能:
function test () {
// statements 1
statement_X;
// statements 2
}
我正在使用瀏覽器的開發工具來瀏覽這些語句。 現在,當我在“ statement_X”處暫停時,我想終止函數執行(我不希望執行函數的“ statement 2”部分),就好像“ statement_X”緊隨其后return;
聲明。
我知道Chrome具有內聯腳本編輯功能,因此我可以在暫停的語句之后手動添加return語句,然后按CTRL + S重新執行整個操作,但是我也需要IE的此功能,因此我希望一般解決方案。
(對於瀏覽器)盡早終止執行似乎是一件容易的事,因此我期望開發人員工具具有這種功能。
我已經在IE 9中成功測試了這一點,因此我將其發布在這里作為答案:在腳本調試器中暫停statement_X
,按F10鍵,以便statement_X
仍被執行,然后右鍵單擊封閉函數的最后一行(該行(用右大括號}
終止函數主體),然后從下拉菜單中選擇“設置下一條語句”。 這將跳過執行直到函數結束仿佛就在后有一個void return語句statement_X
。
如果函數的最后一行上還有其他語句,如下面的代碼所示,請小心僅在大括號上單擊鼠標右鍵,以使此技術起作用。
function test () { alert("statement 1");
alert("statement 2"); } function test2 () { alert("statement 3"); }
對於內聯函數或在不打算用於調試的精簡腳本中,有時可能需要這樣做。
如果我理解正確,您將無法執行此操作。
調試器(無論如何是Chrome的調試器)本身都是基於javascript的。
這些家伙最終使用eval()
來運行注入的代碼。 遍歷Chrome檢查器時,似乎在嘗試評估某些東西時調試器代碼最終會調用此代碼(我認為):
function (evalFunction, object, expression, isEvalOnCallFrame, injectCommandLineAPI)
{
// Only install command line api object for the time of evaluation.
// Surround the expression in with statements to inject our command line API so that
// the window object properties still take more precedent than our API functions.
try {
if (injectCommandLineAPI && inspectedWindow.console) {
inspectedWindow.console._commandLineAPI = new CommandLineAPI(this._commandLineAPIImpl, isEvalOnCallFrame ? object : null);
expression = "with ((window && window.console && window.console._commandLineAPI) || {}) {\n" + expression + "\n}";
}
return evalFunction.call(object, expression);
} finally {
if (injectCommandLineAPI && inspectedWindow.console)
delete inspectedWindow.console._commandLineAPI;
}
}
evalFunction
只是eval()
的別名。
問題是,即使在硬代碼中,我們也不能在eval中使用return語句。 它將始終為您提供SyntaxError: Illegal return statement
。
所以不,沒有伏都教返回聲明。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.