0

Here is the window:enter image description here so now, when I scroll down (the children appear in the same fashion as displayed above, all way long), I see what I want:enter image description here but I just fail to access it. Why?

Here the code, in a function that lies in a js folder:

function update_match(slot, match, s) {
    $("#match" + slot + " i").text(match);
    console.log(window);
    console.log(window.saves1);          // undefined
    console.log(window.external.saves1); // undefined
    (slot == 1) ? window.saves1.item = s : window.saves2.item = s;
}

The variables are created like this:

function set_global(name, pos, ab, needSave, s) {
    window.saves1 = {item: s};
    window.saves2 = {item: s};
}

inside js/main.js file.

The file structure is like this:

index.php (where the php code runs and calls update_match())
js - main.js
   - read_match.js
gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • When are you calling this `update_match` function, from where? [Remember that the `console` is lying](http://stackoverflow.com/q/23392111/1048572) – Bergi Sep 13 '15 at 20:32
  • do you somehow have multiple iframes ? – Lucian Depold Sep 13 '15 at 20:34
  • I am calling it from index.php, at the bottom of it @Bergi. In the directory where the index.php file lies, the js folder also does. – gsamaras Sep 13 '15 at 20:34
  • @LucianDepold not to my knowledge, but I am using jquery mobile, would be might cause a problem? – gsamaras Sep 13 '15 at 20:35
  • no, i don't think so – Lucian Depold Sep 13 '15 at 20:37
  • @gsamaras: OK, it would be nice if you could post that code (but only the HTML that the browser receives, not the PHP source). And where in your code are you creating that `save1` global variable? Please include that as well. – Bergi Sep 13 '15 at 20:38
  • @Bergi I have the php code in the bottom of the index file. The only thing I do relative to jaascript is: `echo "";`. What do you want me to include? Sorry, but I am just not sure. (nice answer btw). – gsamaras Sep 13 '15 at 20:41
  • it could be that while you are running the update_match, the global variables aren't defined yet. they are created later. BUT because console.log, does not echo out a snapshot of the window object at that time, it shows the global variables, because at the end of your script they got created. – Lucian Depold Sep 13 '15 at 20:41
  • @LucianDepold, that might be the case. I will search for a way to run the php code when the page is loaded. – gsamaras Sep 13 '15 at 20:43
  • console.log shows the window object from the end of the execution, not inbetween – Lucian Depold Sep 13 '15 at 20:43
  • could you type console.log(window.saves1); directly into the console ? – Lucian Depold Sep 13 '15 at 20:43
  • @LucianDepold I got: `Object {item: 0} undefined` – gsamaras Sep 13 '15 at 20:44
  • the undefined is not refering to the object. if you type console.log(window), you ll get an undefined at the end too. the object window.saves1 is there ! – Lucian Depold Sep 13 '15 at 20:45
  • @Bergi I updated my post! Lucian, I can see that too, but I want to access it and I can't! – gsamaras Sep 13 '15 at 20:47
  • If possible, could you open up the page and then just click on View Source Code and just paste everything there on the question? – MinusFour Sep 13 '15 at 20:53
  • Well it's my page, so I can do that yes, but the code is too much @MinusFour. – gsamaras Sep 13 '15 at 20:53

1 Answers1

1

You are running update_match too early.

It seems that while you are running the update_match, the global variables aren't defined yet. They are created later. But because console.log, does not echo out a snapshot of the window object at that time, it shows the global variables, because at the end of your script they got created and console.log shows the "finished" window Object.

To solve your issue, run the update_match later, either after the document is ready or using the setTimeout function with a reasonable delay:

setTimeout(function(){ update_match(); }, 500);

To run the function after the document is ready, take look at this post:

jQuery Mobile: document ready vs page events

You could do it by:

$(document).ready(function() { 

update_match();

});
Community
  • 1
  • 1
Lucian Depold
  • 1,999
  • 2
  • 14
  • 25
  • I must pass PHP variables in that function, so I think the only way to go is with an AJAX call. However, all this bug catching made my more hungry than a Snorlax, so I will code it after my meal. Thanks! – gsamaras Sep 13 '15 at 20:54
  • Good idea Lucian! Actually, he gets the effect of that and his special move is the body slam! Now, on our topic, I have some issues, maybe you can take a look: http://stackoverflow.com/questions/32554733/execute-scripts-ajax-returns – gsamaras Sep 13 '15 at 21:31