1

i am having a problem in changing the value of my global variables in javascript...Here's the full code.

//Initialize cordoba
document.addEventListener("deviceready", onDeviceReady, false);

function onDeviceReady(){
    var undefined;
    var phone_number;

    //change phone_number
    checkData();

    //alert the new phone_number
    alert(phone_number);


    function checkData(){
        var db = window.openDatabase("Demo", "1.0", "Demo", 512000);
        db.transaction(selectProfile,DBerror);
    }

    function selectProfile(db){
        db.executeSql('SELECT phone_number FROM profile', [],
            function(db,results){
                if(results.rows.length > 0){
                    //change the global variable(phone_number)
                    phone_number = results.rows.item(0).phone_number;
                }else{location.href = 'index.html';}
            },
            DBerror
        );
    }
}

In every page, i need to get the phone number value from the database, change the global variable (phone_number) to that value and use it through out the whole script. Thanks

3
  • that one is aside, it's for checking if a variable exists and you must define that for old browsers Commented Apr 29, 2013 at 19:13
  • Any reason why you don't simply have a getPhoneNumber() function that returns the phone number? Then you could say var phone_number = getPhoneNumber(); Commented Apr 29, 2013 at 19:17
  • It can't be implemented (or maybe i couldn't figure out a way to do it) because of how the phonegap api works Commented Apr 29, 2013 at 19:46

1 Answer 1

1

Declare phone_number outside of any function should work.

You can also use window.phone_number to guarantee a global scope. But this is considered bad practice? I'm sure someone with much more experience than I can explain why.

Sign up to request clarification or add additional context in comments.

5 Comments

window.phone_number is only bad practice inasmuch as global variables are generally bad practice. The only particular bad thing about using window is that your code will be marginally more difficult to port to node.js, which uses global instead of window as its global.
@Spikes You need to define phone_number outside of any variables AND not define it anywhere else. If you just added a var phone_number at the top of your script but didn't remove the var phone_number inside onDeviceReady, the inner declaration will "shadow" the outer declaration, so setting phone_number sets the local variable, rather than the global one.
@Spikes While my above comment is true, I just read your code more closely and see you're making a classic async mistake, which will also cause your code to fail. The callback in db.executeSql does not run until after the alert runs.
@apsillers; that must be it. I've searched the W3C for a solution but couldn't find any. Can you rewrite the code in a way that it works. I need to get the data from the database before anything even happens on the page. Thanks a lot
@apsillers; I've rewritten the code. Thanks for your suggestion, it really helped.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.