Note, This is just an example of a concept, there are more details you must take into account in a production environment
There is limited information about your development environment (i.e web server software, server-side language used). But here is what I would do in the case of a LAMP (linux, apache, mysql, php) setup.
I create a javascript function to handle write to the remote text file:
function logOnLoad()
{
var sData = "Page has been loaded";
$.ajax({
url:"https://example.domain.com/logData.php",
data: {sData: sData},
method:"post"
}).done(function()
{
alert("Finished Async write to server side file.");
})
}
I then subscribe this function to an event handle (it can be any other event handler though, it's up to you in regards to when you want to write this data), for this example I just use onload on the body tag:
<body onload="logOnLoad()">
<p>test</p>
</body>
So now my javascript function executes onload of the html page. Now we need to write the server side code to handle the ajax request, so I create a php file called logData.php and put this in the file:
<?php
function writeData($sData, $sFilePath)
{
$sTargetFile = fopen($sFilePath, "a");
fwrite($sTargetFile, $sData);
fclose($sTargetFile);
}
if(isset($_POST['sData']))
{
writeData($_POST['sData'], "testLog.txt");
echo "done";
}
Now whenever I load test.html, it takes the static content from the javascript variable sData and does a POST against the logData.php, the logData.php file in return looks for the file specified in the writeData function call (in this example I look for testLog.txt). It creates a file handle from this information and we do a fwrite($sTargetFile, $sData) and this writes the content to the server side text file.