Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Is it possible somehow to prevent users from being able to change JavaScript variables, trough the address bar or with programs like FireBug?

share|improve this question
    
Some insight into what your goal is may help us help you find a tamper-proof solution. I can tell you right now that you will need some help from PHP, ASP or other server-side scripting. –  Paul Lammertsma Apr 19 '10 at 16:27

4 Answers 4

up vote 9 down vote accepted

JavaScript is raw source code being interpreted on the client machine, so the short answer is no. The best you could perform as much work as you can on the server. Any JavaScript code you push to the client can be tampered with by the client.

share|improve this answer

This is one of those questions that pretty much begs the response "What is your ultimate goal?"

As stated by others, no, you can't. JavaScript is client side so entirely in the hands of the client, not you.

Obfuscation, as heavyd mentions, could be useful, but it's certainly not a secure/sure-fire way to prevent it.

If this is something you need for security reasons, then the answer is likely that it shouldn't be done with client side JavaScript in the first place.

share|improve this answer

No, but you can obfuscate your code to make it difficult to read and reverse engineer.

share|improve this answer

You could create private variables and use an accessor method to get at them

var mySingleton = function(){

    /* here are my private variables and methods */

    var privateVariable = 'something private';

    function showPrivate(){

        console.log(privateVariable);

    }



    /* public variables and methods (which can access private variables and methods ) */

    return {

        publicMethod:function(){

            showPrivate();

        },

        publicVar:'the public can see this!'

    }

}

var single = mySingleton();

single.publicMethod();  // logs 'something private'

console.log(single.publicVar); // logs 'the public can see this!'
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.