I am a beginner in JavaScript and currently working on a small HTML page that will be run locally. I have a string in JSON format that I need to store and load as a file on the hard drive.
I have managed to store the string using the following code snippet on Firefox:
function saveJSON() {
var obj = {name:'John', max:100};
window.open( "data:text/json;charset=utf-8," + escape(JSON.stringify(obj)))
}
However, this method only works on Firefox, and I need a solution that also works on Internet Explorer. I have looked into using ActiveX but have not been able to find a suitable example for implementation.
Should I explore the ActiveX route, or is there a more effective HTML/JS solution that is compatible with both browsers?
The second issue I am facing is loading the JSON file. After some research, I discovered that after loading the file, I can convert it into a JSON variable using JSON.parse. However, I am unsure of how to actually load a selected JSON file. I have an
<input type=file id="filePath">
element to obtain the file path (although it returns different values in each browser), and ideally I would like to perform something similar to
var a = loadFile(filePath.value)
If anyone has any suggestions or solutions for how to achieve this, I would greatly appreciate the assistance.
Thank you.