I would like to change my file.txt on click. For example, I 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 this code when I click, for example, redanswer
it is shown the number 11 because it's rans
(10) + 1. But if I reload the page and I click again on the redanswer
it is already shown the number 11 because the file.txt it isn't been edited. I would like to add a piece of code that, on click on the redanswer
, can edit the file.txt like var config={"rans":11,"bans":20}; //10+1
var config={"rans":11,"bans":20}; //10+1
.
I hope you understand. Thank you very much.
Sadly this is not possible. JavaScript does not have read/write capabilities .
I suggest creating a simple POST or GET PHP api to be able to write files.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.