Results 1 to 4 of 4

Thread: Can't modify a Javascript global variable!!

  1. #1
    Join Date
    Dec 2010

    Can't modify a Javascript global variable!!

    I know it sounds weird, guess has to do with lexical scope. Here's the code:

    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <script src="jquery.js" type="text/javascript"></script>
    <script type="text/javascript" charset="utf-8">
    var db = window.openDatabase("CommDB", "", "Common Database", 1024*1000);
    var aGlobalVariable = 'GLOBAL';

    function resultsHndlr(tx,rs) {
    document.writeln('1: {'+aGlobalVariable+'}<BR>');
    window.aGlobalVariable = 'LOCAL';
    document.writeln('2: {'+aGlobalVariable+'}<BR>');

    function f() {
    db.transaction(function(tx) {
    tx.executeSql('SELECT name FROM HeadersTable', [], resultsHndlr);
    document.writeln('3: {'+aGlobalVariable+'}<BR>');

    $(document).ready(function() {

    <body id="dt_example">
    <p>Weird, indeed!</p>

    ... and here's the output in Chrome 7.0.517.44:
    3: {GLOBAL}
    1: {GLOBAL}
    2: {LOCAL}

    As you can see, global variables are indeed visible from resultsHndlr(), but any attempt to modify them, even fully qualified, is kept locally.
    Would greatly appreciate any help, or suggestions around this. I desperately need a '3:{LOCAL}'. Thanks in advance.

  2. #2
    Join Date
    Oct 2008
    Hmmmm... I believe the output there is correct. Let's break it down a bit, the order in which things are happening is the key issue here.
    1. First function executed is f() which also invokes tx.executeSql and passes to it the callback function "resultsHndlr", which will actually be invoked when tx.executeSql is finished I presume. So function f actually makes it to the next line of it's code before resultsHndlr is invoked (this may or may not always be the case, unsure). Thus 3: {GLOBAL} is the first output, and correctly so, as aGlobalVariable has not been altered yet.
    2. After that, then resultsHndlr is invoked within tx.executeSql and outputs 1: {GLOBAL} then changes the value of aGlobalVariable to 'LOCAL' and outputs 2: {LOCAL]

    And so it would seem to be explained.
    Last edited by astupidname; 12-07-2010 at 03:37 AM.

  3. #3
    Join Date
    Dec 2010


    Thanks for your reply, that is exactly what is happening, callback function executes after main function (f()) returns, but I don't know exactly when, so in practical terms, I can't modify a global javascript variable when I need to.
    It's like MS-windows: I need a SendMessage() function, but I only got a PostMessage(), i.e., to modify one lousy global variable, I need a swarm of messages and code. There should be a way around this, shouldn't it?

  4. #4
    Join Date
    Dec 2010
    There is an obvious and distasteful solution: Instead of calling f() once, as it should be, call it twice, i.e. replace:

    $(document).ready(function() {


    $(document).ready(function() {

    Perhaps the worst ever proposed solution of my career. Does anyone knows of a better, more elegant way around this?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center