0

So I have one HTML page which consists of a bunch of form elements for the user to fill out. I push all the selections that the user makes into one global variable, allTheData[] inside my only Javascript file.

Then I have a 2nd HTML page which loads in after a user clicks a button. This HTML page is supposed to take some of the data inside the allTheData array and display it. I am calling the function to display allTheData by using:

window.onload = function () {
    if (window.location.href.indexOf('Two') > -1) {
        carousel();
    }
}

function carousel() {
    console.log("oh");
    alert(allTheData.toString());
}

However, I am finding that nothing gets displayed in my 2nd HTML page and the allTheData array appears to be empty despite it getting it filled out previously in the 1st HTML page. I am pretty confident that I am correctly pushing data into the allTheData array because when I use alert(allTheData.toString()) while i'm still inside my 1st HTML page, all the data gets displayed.

I think there's something happening during my transition from the 1st to 2nd HTML page that causes the allTheData array to empty or something but I am not sure what it is. Please help a newbie out!

6
  • You're calling window.location.href.indexOf('Two'), which is case sensitive. Are you sure that Two is actually part of the second page's URL? Commented May 22, 2017 at 22:31
  • Yep! I believe it works because "oh" gets logged into the console when the 2nd page loads in. Commented May 22, 2017 at 22:52
  • So, where is the second page getting that data from? Commented May 22, 2017 at 23:40
  • @yezzz the second page is connected to the single Javascript file so from the allTheData array there? at least that's what i was hoping for :(. Commented May 22, 2017 at 23:54
  • In a browser, the global scope is the window object. If you replace the window, you replace the global scope. Commented May 23, 2017 at 3:46

1 Answer 1

1

Web Storage: This sounds like a job for the window.sessionStorage object, which along with its cousin window.localStorage allows data-as-strings to be saved in the users browser for use across pages on the same domain.

However, keep in mind that they are both Cookie-like features and therefore their effectiveness depends on the user's Cookie preference for each domain.

A simple condition will determine if the web storage option is available, like so...

if (window.sessionStorage) {
   // continue with app ...
} else {
   // inform user about web storage
   // and ask them to accept Cookies
   // before reloading the page (or whatever)
}

Saving to and retrieving from web storage requires conversion to-and-from String data types, usually via JSON methods like so...

// save to...
var array = ['item0', 'item1', 2, 3, 'IV'];
sessionStorage.myApp = JSON.stringify(array);

// retrieve from...
var array = JSON.parse(sessionStorage.myApp);

There are more specific methods available than these. Further details and compatibility tables etc in Using the Web Storage API @ MDN.

Hope that helps. :)

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

2 Comments

Thanks a bunch! It's just what I needed.
@bobthebuilder4t5 No probs :)

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.