如何在Windows8商店应用程序中的Javascript / HTML5中设置全局变量 [英] How to set global variables in Javascript/HTML5 in Windows8 store app

查看:65
本文介绍了如何在Windows8商店应用程序中的Javascript / HTML5中设置全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在.js文件中使用全局变量。
例如,

I want to use global variable in .js files. For example,

当我读取a.js + a.html中的文件内容并保存到某个变量'fileContent'时

when I read a file content in " a.js + a.html " and saved into a certain variable 'fileContent'

然后我也想在b.js + b.html中使用'fileContent'。

then I also want to use that 'fileContent' in " b.js + b.html ".

(当我更改b.js + b.html中的'fileContent'时,这会影响a.js + a中的'fileContent'。 html)

(also, when I change the 'fileContent' in "b.js + b.html", that should affect 'fileContent' in "a.js + a.html")

我该怎么办?

谢谢。

推荐答案

鉴于Windows 8应用程序的体系结构是单页面模型,您不在其中导航浏览器,而只是加载HTML片段并将其插入当前文档,这很容易。但是,我建议使用一些打字,而不仅仅是裸全局变量。

Given that the architecture for Windows 8 Applications is the single page model, where you don't navigate the browser, but merely load a fragment of HTML and insert it into the current document, this is very easy. I would, however, recommend using some typing, rather than just a "naked" global variable.

文件A(globals.js):

File A (globals.js):

WinJS.Namespace.define("MyGlobals",  {
    variableA: null,
});

在WinJS包含后,将其包含在default.html的顶部。

Include this at the top of default.html after the WinJS includes.

文件B(b.js):

// your other code
...
MyGlobals.variableA = getValueFromSomewhere();

文件C(b.html或c.js):

File C (b.html, or c.js):

// your other code
...
printSomethingAwesomeFromData(MyGlobals.variableA);

这篇关于如何在Windows8商店应用程序中的Javascript / HTML5中设置全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆