How to Disable JavaScript Function Calls from the Browser Console

How to disable JavaScript function calls from the browser console?

Is there a general way to disable functions call from console?

No. there isn't: Never. Well, apparently, Facebook found a way in Google Chrome to do so: How does Facebook disable the browser's integrated Developer Tools? - though, I would consider it a bug :-)

Is it maybe something else that I have ignored?

Yes. JavaScript is executed client-side, and the client has the full power over it. He can choose whether or not to execute it, how to execute it and modify it as he wants before executing it. Modern developer tools allow the user to execute arbitrary functions in arbitrary scopes when debugging a script.

You can make it harder to introspect and use (call) your code by avoiding to expose your methods in the global scope, and by obfuscating (minifying) the source. However, never trust the client. To avoid cheating, you will have to perform all crucial task on the server. And don't expect all requests to come from your JavaScript code or from a browser at all; you will need to handle arbitrary requests which might be issued by some kind of bot as well.

Disable user to call functions from console in web browser

console are execute by the function eval()
so override the eval function by:

window.eval=function(){};

Now console is disabled

But this is also not good idea because it cannot prevent the execution of bookmarklet so use @Anthony's idea.

How to disable JavaScript in Chrome Developer Tools?

Click the gear icon in the corner of the Developer Tools, click Settings, then under Debugger, check Disable Javascript, as shown in the following video:

Sample Image

Disable function call to debugger in Chrome Developer Console

Right-click the line number column next to the line that includes the debugger statement and select Never Pause Here.

Do this for each instance of debugger.

never pause here screenshot

Unfortunately, I don't think there's a way to disable the debugger keyword outright, so I understand that this solution is tedious if you have a lot of calls to debugger, but it's the best we have at the moment. I mentioned to the DevTools team (I'm the DevTools technical writer) the need to be able to disable the debugger keyword outright.

Update A DevTools engineer also reminded me that we have the Force Resume button. Hold the Resume button then select Force Resume and DevTools ignores all subsequent breakpoints.

force resume

(source: google.com)

documentation

If the script is in a separate file, you can also blackbox that script.

How to quickly and conveniently disable all console.log statements in my code?

Redefine the console.log function in your script.

console.log = function() {}

That's it, no more messages to console.

EDIT:

Expanding on Cide's idea. A custom logger which you can use to toggle logging on/off from your code.

From my Firefox console:

var logger = function()
{
var oldConsoleLog = null;
var pub = {};

pub.enableLogger = function enableLogger()
{
if(oldConsoleLog == null)
return;

window['console']['log'] = oldConsoleLog;
};

pub.disableLogger = function disableLogger()
{
oldConsoleLog = console.log;
window['console']['log'] = function() {};
};

return pub;
}();

$(document).ready(
function()
{
console.log('hello');

logger.disableLogger();
console.log('hi', 'hiya');
console.log('this wont show up in console');

logger.enableLogger();
console.log('This will show up!');
}
);

How to use the above 'logger'? In your ready event, call logger.disableLogger so that console messages are not logged. Add calls to logger.enableLogger and logger.disableLogger inside the method for which you want to log messages to the console.



Related Topics



Leave a reply



Submit