Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if 'debugger;' keyword exists?

Tags:

javascript

Sometimes some developers forgot to remove debugger; in javascript code, and it produce javascript error on IE. How can you check (like for the console: if(window.console){console.log('foo');}) if a debugger exists?

BTW: I don't want to detect if the browser is IE, I want a generic method if possible Thanks,

like image 433
JohnJohnGa Avatar asked Mar 24 '11 10:03

JohnJohnGa


2 Answers

You cannot.

The best solution would be adding a hook to your version control system to prevent code containing debugger; statements from being committed/pushed.

Asking your devs to search for debugger; or at least have a careful look at the diff before committing is also a solution - but not as effective as hard-rejecting in the VCS.

like image 135
ThiefMaster Avatar answered Nov 11 '22 12:11

ThiefMaster


You could attempt to compile a function that declares debugger as a local variable. If debugger is reserved as a keyword, the JS engine will throw an error which you can catch.

var debuggerIsKeyword = false;
try {
    new Function("var debugger;");
} catch(e) {
    debuggerIsKeyword = true;
}

However I'm not sure that knowing whether a keyword exists or not is actually helpful.

like image 5
cspotcode Avatar answered Nov 11 '22 11:11

cspotcode