How to set global variables in Javascript/HTML5 in Windows8 store app

前端 未结 4 449
没有蜡笔的小新
没有蜡笔的小新 2020-12-21 19:34

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

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

4条回答
  •  臣服心动
    2020-12-21 20:22

    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.

    File A (globals.js):

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

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

    File B (b.js):

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

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

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

提交回复
热议问题