Search code examples
htmljscript

refresh data read from text file with out page refresh


i am a novice at Java and JS so this will be very basic.

I've got this code that creates a text file in a specific directory. i only got as far as creating an actuale file, however, as the text file will be frequantely updated, i need the page to refresh/reload the text file and display it's data (just in the blank page). How do i do this, with out user needed to click refresh (auto refresh in sense, however, i've tried auto refresh and it does not seem to reload JS and/or display text file's content)

Create Text file/Read/Display content/Refresh and/or Reload - no user refresh

<script>
    function createFile()
    {   
        var object = new ActiveXObject("Scripting.FileSystemObject");
        var file = object.CreateTextFile("C:/Documents and Settings/galimbek.sagidenov/My Documents/Practice HTML_Photoshop_java/BroadcastTest.txt", false);
        file.WriteLine('Hello World');
        file.WriteLine('Hope is a thing with feathers, that perches on the soul.'); 
    file.Close();
    }
</script>


Solution

  • this will not accomplished by using client side javascript only you have to use server side code:

    server ex (using node.js):

    server :

    var http = require("http"),
        fs=require("fs");
    
    http.createServer(function(request, response) {
     fs.writeFileSync("C:/Documents and Settings/galimbek.sagidenov/My Documents/Practice HTML_Photoshop_java/BroadcastTest.txt", 'Hello World\r\nHope is a thing with feathers, that perches on the soul.');
    
    }).listen(8888);
    

    client

    <script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
    <script>
    $(function(){
        $.get("http://localhost:8888",function(){
            console.log("writing to file successeded");
        })
    })
    </script>