-1

I'm making some html files for fun, and i am trying to make a social-media thing, and the only idea that came me was to write html using javascript and the only thing i found that work is document.write() but it does not save in the html file. Is it atleast possible? If it is.. how can you do it? (or with anything else)

EDIT : i do not mean to make another file, i want to modify the one i run the javascript (or anything else) script that will SAVE the source code.

EDIT 2 : please ignore this question. thanks

Ad2017
  • 19
  • 2
  • 10
  • No. You will need AJAX for that. – BenM Sep 08 '18 at 10:20
  • Possible duplicate of [Using HTML5/Javascript to generate and save a file](https://stackoverflow.com/questions/2897619/using-html5-javascript-to-generate-and-save-a-file) – Aankhen Sep 08 '18 at 11:38
  • You can make a chrome extension which take permission and store the file on disk, check this https://codemirror.net/ all you need is implement it in chrome web extension. – Anirudha Gupta Sep 08 '18 at 12:29
  • @Aankhen i do not want to generate a file, i want to replace the existing one – Ad2017 Sep 08 '18 at 14:42
  • Are you talking about using JavaScript _on the client_ to change the HTML file _on the server_? If not, and if you want to change a file _on the client_, it’s up to the user what they save it as. You don’t have any control over the filename, hence the duplicate. – Aankhen Sep 08 '18 at 15:02
  • oh.. well then nevermind – Ad2017 Sep 08 '18 at 15:34

2 Answers2

0

The only way to have access to clients computer using JavaScript is NodeJS. With regular browser JavaScript, you cannot save files on the client's computer. If you want to stay with browser document.write or DOMElement.innerHTML = <div></div> are your options.

Ertan Kara
  • 308
  • 3
  • 12
-1

It is possible. The basic principle is that you can give a unique id for each HTML element (e.g. ,); and then using document.getElementById command to get the element and then set the innerHTML or textContent attribute to the string that you want.

Here is an example:

<html>
  <body>
   <div id="pp">
   </div>
   <div id="qq">
   </div>

   <button onclick="changeText()">Update the text</div>
   <script>
      function changeText()
      {
          var div1=document.getElementById("pp");
          var div2=document.getElementById("qq");
          div1.innerHTML="Hello";
          div2.textContent="World";
      } 
   </script>

However, there are so many frameworks can make your life easier.

The KNVB
  • 3,588
  • 3
  • 29
  • 54
  • yeah but it does not save the text in the html. i meant to MODIFY the html and add text, not just for one instance. – Ad2017 Sep 08 '18 at 14:44
  • As Ertan said that, "With regular browser JavaScript, you cannot save files on the client's computer.", by the way, would you tell us what do you want to do? Otherwise, we cannot help you. – The KNVB Sep 09 '18 at 09:33
  • as i said in the post, i wanted to make a social media thing, and the only idea that came me was to make a prompt window to enter the message you wanted to share, and a script that would add the text to the html. maybe there are other alternatives but this is the one that came into my head – Ad2017 Sep 09 '18 at 10:11
  • do you want to build it by yourself? Or using an open source solution? – The KNVB Sep 10 '18 at 02:14
  • myself because i do it on a html file – Ad2017 Sep 14 '18 at 19:52
  • It cannot be implemented by HTML and javascript only. You need an application server. You need to write a server-side program. – The KNVB Sep 15 '18 at 10:13