Saving changes in javascript file - javascript

I have a html code that creates an input form on the browser and a javascript file that creates an array.The javascript program takes the input value and add it to the array and after display it on browser.
window.alert("Add some text")
var x;
var arr=["text1","text2"]
function myFunction() {
x=document.getElementById('input').value;
x=x.toLowerCase();
var y=arr.indexOf(x);
if (y!=-1) {
window.alert("You have added this text before");
} else {
arr.push(x);
document.getElementById('demo').innerHTML=arr ;
}
}
The problem is that when I exit the file(the web page displayed on my browser) I lose the inputs.Let's say that I add "text3" and the program display text3,when I exit the browser I lose the input "text3".
What can I do to save the inputs and display them even if I exit the program and I enter again?
Can I make two javascript file one with the array(like a database) and one that takes the input and save it in the other javascript file,and if so how do I make it?

As javascript is client side you will never be able to store permanent data as the script will be loaded (reset) after any refresh. Are you aware of the html5 localstorage concept? Saving data from js to the browsers storage space.
Edit:
Quick start on localstorage - http://www.w3schools.com/html/html5_webstorage.asp

Related

Web application - Write To .txt file with Javascript FileObject.Write

I'm trying to re-purpose some older java script code that I use in order to create a text file via an intranet web app. The following code creates text files that are picked up and processed by a third party application running on the same device...
<script>
function WriteTransferFile(location) {
var fileSystemObject = new ActiveXObject("Scripting.FileSystemObject");
var fileObject = fileSystemObject.CreateTextFile("C:\\transfer\\transfer.txt", true);
fileObject.Write(location);
fileObject.Close();
return location;
}
</script>
<script id="writetransfer">
function writetransfer() {
var Location = document.getElementById('lblCommand');
var Command = Location.innerHTML;
WriteTransferFile(Command);
}
</script>
This works fine for a single text string to be pasted into a text file, but I now need to create a file which takes 4 different fields from the web page and writes them on to consecutive lines. I've played around with the above but any attempt to add a second line of text overwrites the first line instead creating a new one below it. Desired output...
Field 1
Field 2
Field 3
Field 4
Any ideas how I might accomplish this?
I'm aware this probably isn't the sleekest solution, but there are no concerns regarding the suppression of active X security settings as this is for a closed system with access to local intranet only.
Thanks
Steve

How can I extract dialog text using javascript and iMacro on firefox browser

I am in big problem. I have to update person information from a csv file. But I also need to save status when the person Id was searched. It can be already updated, incorrect or may open a per populated form.
I need to extract that dialog text and save in a status.csv file. If no error occured then I can update the form and submit it, and again the text in the last dialog can be extracted and saved in a status.csv file.
Is there any solution for extracting dialog text with iMacro and js on Firefox. The solution link given on the iMacro wiki didn't work for me.
This question is not useful: not a solution for firefox
Perhaps this solution would work for you:
// imitation of javascript dialog
var ret = iimPlayCode('URL GOTO=javascript:{alert("Dialog<SP>text<SP>to<SP>be<SP>extracted");}')
// extracting dialog text
if (ret < 0)
var dlgText = iimGetErrorText();
dlgText = dlgText.match(/Dialog message: "(.*)",/)[1];

Toggle JSON file i read in

I have a function that runs my code given that I pass through a JSON file. I did this so I can change the file via a button not via code.
tryOutChange(JSONFILE) // this holds the main bulk of code.
changeJSON() // this is a button that runs the function above.
This function first deletes the old 'g' elements that hold the nodes and edges (my force directed graph) and then runs the main code with a different JSON file. This way I can click the same button and swap which JSON file i read.
var networkJSONfile = true;
function changeJSON()
{
var oldForce = document.getElementById("forceLayout");
oldForce.parentNode.removeChild(oldForce);
if(networkJSONfile == true){
tryOutChange(engine);
networkJSONfile = false;
} else if(networkJSONfile == false){
tryOutChange(network);
networkJSONfile = true;
}
}
This works. But only once. When I go to create one of the graphs i recently deleted it wont create the graph again ? Have I deleted the JSON file too by anychance or ?
Network and Engine are JSON files
Here is what happens.
I run the tryOutChange() and pass the 'network' JSON file to it straight away so it comes up with a network viewer.
I then click a button which runs the changeJSON() function.
This function deletes everything I created previously when I called tryOutChange(). and then calls that function again with Engines JSON.
When I click the button again it does the same this. Deletes what I have created, then called that tryOutChange() function using the Network JSON. And so on. So in theory, it should toggle JSON files from network to engine on click.
When i first click it, its fine, it deletes the network and shows the engine, but when i want to go back and recreate the network uising network JSON it doesnt work ?

save to localstorage dynamic JQuery rows

I think I have a major fault in my webpage design. I need to save the content if I reboot the computer or close the webpage.
I do not have access to any type of a database server only MS Access. I was thinking of utilizing localstorage as the page will be constantly viewed from the same computer.
I found this example [link]Edit functionality using javascript and local storage however I am not sure if it will work.
Can someone look at my example and let me know if I can do this or if I need to abandon this and start over.
$(document).ready(function () {
var id = 0;
// Add button functionality
$("table.dynatable button.add").click(function () {
id++;
var master = $(this).parents("table.dynatable");
// Get a new row based on the prototype row
var prot = master.find(".prototype").clone(true);
prot.attr("class", "")
prot.find(".id").attr("value", id);
master.find("tbody").append(prot);
});
// Remove button functionality
$(document).on("click", "table.dynatable button.remove", function () {
$(this).parents("tr").remove();
});
});
http://jsfiddle.net/deaconf19/csL68/
Thanks
If you are running the web page from a web-server -- that is, your URL begins with http:// or https://, then yes, you can use localStorage to save data.
If you are running the file directly from your hard drive, that is, your url begins with file:, then you'll have trouble. Chrome won't let you use localStorage -- I don't remember if Firefox does.
(If you need to run this from your local hard drive and not a server, and you are running under Windows, you can turn the file into an HTA and save data to a regular file.)

WP8 App misbehaving due to StreamWrite in JavaScript

I would like to save the results calculated on html page in a textfile using javascript.
<script type="text/javascript">
window.onload = function () {
var sw : StreamWriter = new StreamWriter("HTML_Results.txt");
sr.Write('xyz");
*** calculations ******
sr.Write (result);
}
</script>
by doing this, my WP8 App is misbehaving and not displaying images as usual. This app is an Image Fader (calculates FPS).
Also tried:
StreamWriter sr;
try {
sr = new StreamWriter("\HTML5\HTMLResults.txt");
sr.Write("xyz");
File.SetAttributes("HTML5\HTMLResults.txt", FileAttributes.Hidden);
} catch(IOException ex) {
console.write ("error writing"); //handling IO
}
The aim is to:
Extract calculated values of several html pages(after getting loaded
one by one) in a single text file.
A Resultant HTML that reads this
text file and displays results in a tabular format.
Is there a better way to this job or the above can be rectified and used? Appreciate help.
Perhaps I've misunderstood your code but it looks like you're trying to write Java within JavaScript scripting tags. You cannot write Java in an HTML document. As far as I know, client-side JavaScript (which given your <script> tags is I guess what you're trying to write) can't perform the kind of file I/O operations you seem to want here.
You need to use Node JS to use JavaScript for something like that and then you're talking server-side. The closest you can get on client-side is using the new localStorage feature in HTML5 (not supported by all browsers).

Categories