Javascript - save/update content

Asked

Viewed 4,599 times

3

Good staff my question is the following: I would like to know if it is possible to update the contents of a file in general.

Example: I have a file called html name. and inside him I have a <h1>nome</h1>, I would like to do a function that when I want by javascript I can modify from 'name' to 'bomdia' and save the contents so that when re-opening the file html name. be written: <h1>bomdia</h1> Is it possible to do this with javascript? some example of how to do?

  • Managed to solve?

3 answers

4

Yes, it is possible.

You can use LocalStorage to store local content via method setItem(), and get it back via getItem(). The code below exemplifies this functionality:

  function dataSave(){
    var refVal = document.getElementById("valInput");
    localStorage.setItem("valor", refVal); // Salva o valor
    dataGet();
  }

  function dataGet(){
    var valorTemp = localStorage.getItem("valor") || 'nome'; // Carrega o valor, 
                                                             // com um default caso 
                                                             // este não exista
    
    document.getElementById('template').innerHTML = valorTemp;
    document.getElementById('valInput').value = valorTemp;
  }
<body onLoad="dataGet();">
  <h2 id='template'>placeholder</h2>

    <input type="text" id="valInput" placeholder="Valor a salvar">
    <button onClick="dataSave();">Salvar</button>
</body>

(For turning in mode sandbox, this snippet will not work in the Stackoverflow editor. Save this content to a page on the local server.)

  • This is a solution with numerous poréms that you have not explained. This only works if the user always uses the same browser, if the user does not clean the data saved in that browser and if the . html is opened via an http server because it opens directly (file:///) blocks localStorage.

  • 3

    I appreciate the justification, @Brunorb. As for your points: The OP is explicitly focusing on JS - database in no time is mentioned as a prerequisite for the answer. Second, I explicitly mention that content should be served via a local server, not directly via protocol file.

  • Look at the guy’s question he seems to be a beginner and does not even know what he wants to do, so I think in an answer of this type of code release generates the following situation: he will try to open . html won’t work and it will open a new question about why it doesn’t work. It will do work behind httpserver but eventually will wipe the data from the browser or use on another or even open the file on another computer and will no longer work and will open a question about why it stopped working.

  • 3

    @Brunorb as a rule I do not assume that the user has doubts not expressed in the question, thus avoiding answers that leave the focus of the proposed question and saving both my time as the OP - and future readers as well.

0

Only with HTML/javascript no. HTML is a static set of tags and text, the only way you can "save" a change to a file of this type is to edit it, either with a text editor or with a programming language type C, php, etc. OK you have javascript available, but this does not help you since the html file is on an external server and JS runs directly in the browser, distinct places. Even if you’re opening yours. html locally could not change the file as a number of restrictions are imposed by browsers for what javascript code can do on the user’s machine, and changing files is one of them.

If you are loading html through an http server (apache, Nginx) it would be possible to do a gambit with localStorage, but it would be quite limited and would not actually change the file (so I will not detail).

0

As Brunorb said, you probably won’t be able to do what you want using only HTML/Javascript. But if your page . html is on your server and you have access, you could change it using javascript only, but it wouldn’t be the user’s browser engine, it would be through Node.JS, which is Chrome’s Engine V8 running on the server.

If you want to change the source code of a page, you basically have two alternatives, or change it on the server, before sending as response to the client (in this case you could use Nodejs), or you dynamically change the client using Javascript, which is actually not a real change in the source code but only a manipulation of the document in the client.

You could also do something complex, like create a copy of the file. html in the client’s Storage using Fileapi with another series of processes that would get more or less the result you expect, but I think that’s not quite what you want. https://scotch.io/tutorials/use-the-html5-file-api-to-work-with-files-locally-in-the-browser

Finally, the javascript code that you run in the client’s browser does not have direct access to your files on the server, that is, you could not use only it to modify your source code. But there are ways to get this result, modify the source code of a web page with javascript, but this involves more technologies than just HTML/Javascript running in the browser.

Browser other questions tagged

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