There is a page source code:

http://jsfiddle.net/u990uzjf/

Next, I perform the following actions:

In the txt text box, I enter the text '777' and click the 'Save' button. After these actions, I reload the page, and it should contain the following source code:

http://jsfiddle.net/u990uzjf/1/

As you can see, the source code contains the text '777' in the text field. How can this be done NOT through PHP, but through Javascript or something like this?

    2 answers 2

    You can use, for example, sessionStorage or localStorage .

     var txt=document.getElementById("txt"); txt.value=sessionStorage["txt"] || "?"; //вызывается при клике на кнопку window.go=function(){ sessionStorage["txt"]=txt.value; } 

    Jsfiddle

    @ Nikita ___ if you need to change the page on the server, then you cannot do without a program that runs there. It's easy to do in PHP

     if(isset($_GET['txt'])){ file_put_contents("test.html", preg_replace( '/(id="txt".*?value=")\K.*?(?=")/s', $_GET['txt'], file_get_contents("test.html")) ); } echo file_get_contents('test.html'); 

    test.html:

     <form action=""> <input type="text" class="text" id="txt" name="txt" maxlength="5" value=""> <input type="submit" class="text" id="button" value="Сохранить"> </form> 
    • Not. I'll try to explain to you in more detail. I write in the text field, for example, '777' // I click on the 'Save' button // I close the file // I open the file again ... and '777' should already be entered in it, i.e. they must be written into the source code. - nick
    • @ Nikita ___ change the source code using only one javascript is possible only if the page is on the local machine, using the FileSystem API. If you need the value to remain all the time, but only for the one who pressed the "Save" button, then you need to replace sessionStorage with localStorage in my code. - Im ieee
    • Not really. I need any user to log in and change this value for everyone. This file will continue to be on the Internet. - nick
    • Well, if it is impossible through Javascript, please tell me how to do it in any other way. But still, it is desirable for me that it was not PHP))). - nick
    • @ Nikita ___ and which server-side languages ​​does it support? Once again I want to note that without them, alas, to write something to the file will not work. - Im ieee

    Nothing happens in JavaScript. Storage is only in the browser. Cookies, sessionStorage, localStorage WebSql if you do not want to shine with a real name, you can write MD5 there or your encryption mechanism. And without php - python, java, perl, ruby, nodJs