1

I have this simple JavaScript code. I have a problem; the first time I launch the page the script I do not have the desired behavior, but when I refresh the page I have the right info.

Honestly I have no idea why I have this concern.

I wanted to share this code in a jsfiddle but the problem does not appear. The problem is present on Chrome and Firefox but not on Edge

//AJOUT TABLEAU FIN
var tableau = [1, 2];
tableau.push(3);
tableau.push(4, ['a', 'b', 'c']);
console.log('ajout fin:', tableau);

//AJOUT DEBUT TABLEAU
tableau.unshift('ajout', 'au', 'debut');
console.log('ajout debut :', tableau);

//SUPPRESSION PREMIER ELEMENT
tableau.shift();
console.log('suppression premier element:', tableau);

//SUPPRESSION DERNIER ELEMENT
tableau.pop();
console.log('suppression dernier element:', tableau);

enter image description here

The first part is when I launch the direct script via its URL, the second part is when I update the page or when I arrive on the page by clicking on a link from another page before.

I know it's really strange and I would have like to know if there was an answer

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
  • If you run just the script you posted and the only thing in your HTML is the script tag, do you still have the problem? – PM 77-1 Mar 31 '20 at 15:34
  • It's not clear what you're expecting, as I don't speak ... French? (I'm assuming the comments and log text say something about what was expected). – Heretic Monkey Mar 31 '20 at 15:34
  • 1
    You may also wish to use `JSON.stringify(tableau, null, 2)` in place of `tableau` in your `console.log`s so that it's more apparent what was in the array at the time of logging. – Heretic Monkey Mar 31 '20 at 15:36
  • @pm77 j'ai isolé ce code dans un fichier simple html et je n'ai affiché ici que la partie js pour eviter les fioritures. Pour mes tests je ne dispose que d'un fichier html avec simplement ce script This is necessarily a bug because when I launch the script (the page) via its url is that the chrome console is open I have the right result if on the other hand I launch the page but the console is closed and I open I don't have the right result ... – Pixel online Mar 31 '20 at 15:58
  • @hereticMonkey Thanks JSON.stringify + JSON.parse are perfect! I think console.log is async?? – Pixel online Mar 31 '20 at 16:14
  • 1
    It has more to do with how Chrome "helps" when logging non-primitive values. See the answers to [weird array behaviour in javascript](https://stackoverflow.com/questions/49838597/weird-array-behaviour-in-javascript) for more. – Heretic Monkey Mar 31 '20 at 16:17
  • @hereticMonkey Thanks it's perfect ;) I didn't think I had an answer because I was having trouble explaining the problem. so I tried to simplify my script as much as possible to make the problem more visible, your answer helped me a lot, thank you again – Pixel online Apr 05 '20 at 08:54

2 Answers2

0

Just load js after document is loaded into DOM

window.onload = function() {
var tableau = [1,2];
tableau.push(3);
tableau.push(4,['a','b','c']);
console.log('ajout fin:',tableau);

//AJOUT DEBUT TABLEAU
tableau.unshift('ajout','au','debut');
console.log('ajout debut :',tableau);

//SUPPRESSION PREMIER ELEMENT
tableau.shift();
console.log('suppression premier element:',tableau);

//SUPPRESSION DERNIER ELEMENT
tableau.pop();
console.log('suppression dernier element:',tableau);
}
Ferin Patel
  • 3,424
  • 2
  • 17
  • 49
  • I still have the same problem, It is very strange it appears when I enter the url of the html file live, but when I update the file or when I access it via a link I do not have the problem. So it does not come from the dom or the loading after the dom it looks like a bug because if it was the dom I will always have the same problem with the refresh because the loading would always be the same – Pixel online Mar 31 '20 at 15:53
0

1) Go to Developer Tool Settings in Chrome, and uncheck:

  1. "Enable JavaScript source maps"
  2. "Enable CSS source maps"

Then refresh Chrome.

2) If you have the Adblock disable it to get rid of the error.

Frank Pentangeli
  • 336
  • 2
  • 6
  • 20
  • Hello, in fact the error I posed was not for the warning;) I know that the warnings come from my chrome extensions but thank you anyway – Pixel online Mar 31 '20 at 15:51