0

I have declared some global variables along with global array in my firstpage.hmtl's script and these global variables and array are accessible everywhere on this page and working fine on this page. And I want to use the values of window.arr[] on all pages of the application but it is showing undefined on all other pages of application

When I redirect from firstpage.html to secondpage.html then I try to get the values of these global variables but here the issue arise because just window.name is working on second page other than that every global variable's value is undefined

I have tried many names for these global variables but just one of them is working which is window.name other than that everything is undefined.

firstpage.html

    window.name = "john";
    window.car = "BMW";
    window.arr = [];

    $(".hotels").click(function () {
            arr.push('hotels');
            console.log("name = " + window.name); //working, prints "name = john"
            console.log("car = " + window.car); //working, prints "car = BMW"
            console.log("arr = " + window.arr); //working, prints "arr = hotels"
    }

secondpage.html

$(document).ready(function () {
    console.log("arr = " + window.arr); //not working here, prints "arr = undefined"
    console.log("car = " + window.car); //not working here, prints "car = undefined"
    console.log("name = " + window.name); //working, prints "name = john"

});
7
  • 1
    I think you want to try session storage for what you're trying to do. developer.mozilla.org/en-US/docs/Web/API/Window/sessionStorage Commented Jan 23, 2018 at 19:31
  • 3
    Global variables are not expected to be shared between page loads; Commented Jan 23, 2018 at 19:31
  • 1
    Every page has it's own window instance. Take a look at using localStorage Commented Jan 23, 2018 at 19:32
  • Also not a good idea to use window.name for a variable as it is also used by browser and should be reserved for unique windows Commented Jan 23, 2018 at 19:33
  • @JasonB thanks man session storage is the suitable solution in my situation Commented Jan 23, 2018 at 19:45

1 Answer 1

1

You have two different pages, you cannot pass data like this. But you can save it in localStorage f.e. and use it from there.

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

Comments

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.