admin管理员组

文章数量:1332359

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'

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

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

What should I do?

Thank you.

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'

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

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

What should I do?

Thank you.

Share Improve this question asked Jan 4, 2013 at 14:29 kladosklados 78712 silver badges38 bronze badges 1
  • im not sure about windows 8 - but you could use stomething like localStorage or sessionStorage in javascript – Andrew Hall Commented Jan 4, 2013 at 14:31
Add a ment  | 

4 Answers 4

Reset to default 6

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, remend 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);

You can also use App Settings:

var applicationData = Windows.Storage.ApplicationData.current;
var localSettings = applicationData.localSettings;
var posite = new Windows.Storage.ApplicationDataCompositeValue();
posite["intVal"] = 1;
posite["strVal"] = "string";

localSettings.values["exampleCompositeSetting"] = posite;

Here You can find more information: Link

You can easily define a variable and can use it as a global variable,for example

WinJS.Namespace.define("MyGlobals", {i: 0,k:5    })

For checking you can add an event listener like

div1.addEventListener('click', divClickEvent);

function divClickEvent() {
        MyGlobals.i++;
        MyGlobals.k++;
        console.log("ValueOf_i____" + MyGlobals.i);
        console.log("ValueOf_k____" + MyGlobals.k);
    }

Use localStorage.
In A:

localStorage.setItem('fileContent', 'value;');

In B:

var fileContent = localStorage.getItem('fileContent');

Or just access the localStorage like any other object:

localStorage.fileContent = "value";

However, keep in mind that the localStorage converts all values to strings, this includes objects and array. To set / get those, you'll need to use JSON.stringify and JSON.parse, respectively:

localStorage.setItem('fileContent', JSON.stringify([1,2,3,4,5,6,7]));
var fileContent = JSON.parse(localStorage.getItem('fileContent'));

本文标签: windows 8How to set global variables in JavascriptHTML5 in Windows8 store appStack Overflow