简体   繁体   English

JavaScript 中未定义的窗口变量

[英]window variable undefined in JavaScript

I'm having a problem with a window level variable.我遇到了窗口级别变量的问题。

When I save data in that variable and try to read that data from it, it gives me undefined.当我将数据保存在该变量中并尝试从中读取该数据时,它给了我未定义的信息。

The following is the code:以下是代码:

window.cart;

function save(name, sku)
{
    window.cart = "sku: "+sku+", nombre: "+name;
};

function read()
{
    console.log(window.cart);
}

You need to call your function if you want to access to them and add a onload event to call this when windows body are loaded.如果您想访问它们,您需要调用您的函数并添加一个 onload 事件以在加载 Windows 主体时调用它。

 function save(name, sku) { window.cart = "sku: "+sku+", nombre: "+name; }; function read() { console.log(window.cart); } document.body.onload = function(){ save(20, 'sku_new'); read(); }

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

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