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
4 Answers
Reset to default 6Given 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
版权声明:本文标题:windows 8 - How to set global variables in JavascriptHTML5 in Windows8 store app - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742316325a2451889.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论