How to create txt with js?

Asked

Viewed 21,710 times

2

And guys I have a JS that has some functions that takes the name of the player, position, position, level, image etc. I want to save this data in a dB, the user logs in with the account and access the data with a JS, to view the information.

Can I do this with a JS and php?

Or I can write to a txt, and capture with JS in the BD folder of the site host.

There is how to create a txt com on the site host with pure JS for example creates writes le and erases data in txt with JS.

  • 1

    Just to be clear: do you want to do this on the server ("site host") or on the client ("site guest")? Carlos' answer seems to me to be a solution on the client side (and exclusive of IE, if I’m not mistaken), others would use local Storage or Indexeddb... A server-side solution would require a platform of its own (whether PHP or any other language, including Javascript - Node.js). Related: "Browser storage"

  • Wanted to use on the server where could only edit the file with login.

  • 1

    In my opinion this is a very broad subject to be addressed in a single question (because it involves programming on the server side, logging in, saving data, restricting access). I suggest we start with some PHP tutorials (or another platform, depending on what your server supports), they will certainly cover a lot of your questions. Also read on AJAX (because it seems to me that you are trying to make a kind of game, so you must need this - or websockets, but then it is already more complicated).

2 answers

3


Javascript is a client-side language and therefore very limited in terms of access to client files. This is for security reasons, to prevent programs from making attacks on the user.

There is a library that saves files via Javascript and works on modern browsers. It is based on HTML5 support, which older browsers do not have, and uses the "Save as" ("Save as") that the browser exposes to Javascript.

Take a look at Github: https://github.com/eligrey/FileSaver.js
A demo here: http://eligrey.com/demos/FileSaver.js/

  • yes Only I wanted the file to focus on a server, to serve as a database, since js has no connection to mysql I thought I could simulate a database with a txt, I’m tempted with ajax, but your answer helped me a lot vlw.

  • @adailtonmoraes uses AJAX to connect to the server. Using a file as a database for JS is not feasible. My answer is in answer to the question you asked about how to create a JS file.

  • ok I will use ajax but only saw the ajax be used with Formularios and I need to use this data in variables js, to read the data, in variables.

  • @adailtonmoraes can send whatever you want with AJAX. Accept an object with whatever you want. Ask another question if you need help. If my answer solved this question you can mark as accepted.

  • Here is no accepted option and there is no way I can score.

  • @adailtonmoraes takes a look here: http://meta.pt.stackoverflow.com/questions/1078/como-e-por-que-aceitar-uma-reply

  • Ready to take advantage of your answer

Show 2 more comments

0

Yes, you can do it with Javascript.(Exclusive for IE) I’ll explain:

//Cria Objeto ActiveX
var dados = new ActiveXObject(“Scripting.FileSystemObject”);

//Função para gravar o arquivo
function GravaArquivo(arq,texto){
//pasta a ser salvo o arquivo
var pasta=”C:/Documents and Settings/Computador2/Desktop/LerGravartxtcomJS/”;
//se o parametro arq que é o nome do arquivo vier vazio ele salvará o arquivo com o nome “Sem Titulo”
if(arq==””){arq=”Sem Titulo”;}
//carrega o txt
var esc = dados.CreateTextFile(pasta+arq+”.txt”, false);
//escreve o que foi passado no parametro texto que é o texto contido no TextArea
esc.WriteLine(texto);
//fecha o txt
esc.Close();
}
//Função para abrir o arquivo
function AbreArquivo(arq){
//o parametro arq é o endereço do txt
//carrega o txt
var arquivo = dados.OpenTextFile(arq, 1, true);
//varre o arquivo
while(!arquivo.AtEndOfStream){
//escreve o txt no TextArea
document.getElementById(“texto”).value = arquivo.ReadAll();
}
//fecha o txt
arquivo.Close();
}
  • He had said on another forum that this method had been blocked in the new browsers so he had not tested it. But I wanted to use the data in a variable for example take the name of the subject position x, and position, to manipulate in variable, and another function to record in txt at a certain time with a settimeout, but valleys already helped a lot vlw.

  • Is Activex exclusive to Internet Explorer, or am I mistaken? (if yes, it should be mentioned in the reply, because the AP did not mention platform)

  • Activexobject is for Windows. For other platforms you have to do a few tricks. In the past, to work on Linux, Wine was used. But that was in the old days, back in 2004 ~ 2009. I can’t say what is used today. And don’t forget that we have also, OS-X (Mac) and iOS and Android present in the market.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.