简体   繁体   English

在其他HTML页面上使用Javascript全局数组时,其定义变得不确定

[英]Javascript global array becomes undefined when used on other html page

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. 我已经在firstpage.hmtl的脚本中声明了一些全局变量以及全局数组,并且这些全局变量和数组可以在此页面上的任何位置访问,并且可以在此页面上正常工作。 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 我想在应用程序的所有页面上使用window.arr []的值,但在应用程序的所有其他页面上显示为undefined

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 当我从firstpage.html重定向到secondpage.html时,我尝试获取这些全局变量的值,但是这里出现了问题,因为只有window.name在第二页上工作,而不是每个全局变量的值都未定义

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. 我已经为这些全局变量尝试了许多名称,但是其中只有一个正在运行,它是window.name,除了所有的东西都是未定义的。

firstpage.html 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 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"

});

You have two different pages, you cannot pass data like this. 您有两个不同的页面,您不能像这样传递数据。 But you can save it in localStorage fe and use it from there. 但是您可以将其保存在localStorage fe中并从那里使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM