javascript - How to change permanently an external file.txt? -
i change file.txt on click. example, wrote this:
html:
<script type="text/javascript" src="file.txt"></script>
jquery:
var counter = config.rans; var counter1 = config.bans; $('.redanswer').one('click', function(){ var xhr = new xmlhttprequest(); xhr.open('put','file.txt', true); xhr.setrequestheader('content-type','text/javascript; charset=utf-8'); xhr.send( 'var config={"rans":' + ++counter + ',"bans":' + counter1 + '};' ); $( '.rtotal' ).text( counter ); $( '.btotal' ).text( counter1 ); }); $('.blueanswer').one('click', function(){ var xhr = new xmlhttprequest(); xhr.open('put','file.txt', true); xhr.setrequestheader('content-type','text/javascript; charset=utf-8'); xhr.send( 'var config={"rans":' + counter + ',"bans":' + ++counter1 + '};' ); $( '.btotal' ).text( counter1 ); $( '.rtotal' ).text( counter ); });
file.txt:
var config={"rans":10,"bans":20};
with code when click, example, redanswer
shown number 11 because it's rans
(10) + 1. if reload page , click again on redanswer
shown number 11 because file.txt isn't been edited. add piece of code that, on click on redanswer
, can edit file.txt var config={"rans":11,"bans":20}; //10+1
.
i hope understand. thank much.
sadly not possible. javascript not have read/write capabilities.
i suggest creating simple post or php api able write files.
Comments
Post a Comment