简体   繁体   中英

How to prevent user from executing javascript code from console?

I am storing some sensitive data on javascript side. How can I prevent user from tampering these data. I can not do this on server side with ajax or anything else, as smoothness of the animation concerned.

Make your JavaScript inaccessible to someone using function scope etc. This isn't perfect but it can certainly make things a lot harder. For example, if you define:

var f = function() { return "test"; }
f();

Then it's easy to call window.f()

If instead you define as a self invoking function then you've just made it much more difficult for someone to call:

(function() { 
return test;
})();

This principle can then be extended - any variables defined within the function, will only have scope within it making it very difficult (but probably not impossible) to get hold of them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM