0

I have the following function, that takes a string from an input, and puts it inside a global array, but as I go to the next page, the global array gets empty, as if nothing happened.

checkItems = [];
function addToCart() {
    "use strict";
    var p = document.getElementById('buy-item-input').value;
    var verifyItem = 0;
        /* some other code... */
        checkItems.push(p);
        location.href = 'pay.html';
}

I've put console.log before chaning the window just to verify that the array gets the data before going to the next page, and it does, but when I try it again at the next page, it's gone...

Voxito
  • 33
  • 8
  • As HTTP is stateless, every time you load the page it will use the initial values of whatever you set in JavaScript. You can't set a global variable in JS and simply make that value stay after loading the page again. – Marco Veríssimo Jun 04 '18 at 14:08
  • I guess your website is simply not a [SPA](https://en.wikipedia.org/wiki/Single-page_application) and therefore everything starts new when clicking a link. – ASDFGerte Jun 04 '18 at 14:08
  • you could use cookies or a database to store the value and then retrieve it when neede. – wayneOS Jun 04 '18 at 14:10
  • Possible duplicate of [Persist variables between page loads](https://stackoverflow.com/questions/29986657/persist-variables-between-page-loads) – Sebastian Simon Jun 04 '18 at 14:23

1 Answers1

0

You can store the value in another place using Query String, Web Storage, Cookies or Windows Name

Check this answer for more info:

Persist variables between page loads