www.webdeveloper.com
Results 1 to 5 of 5

Thread: reading text file in AJAX

  1. #1
    Join Date
    Nov 2010
    Posts
    2

    reading text file in AJAX

    Hi. I am reading a text file using the following Javascript code.

    function getXmlHttpRequestObject() {

    if (window.XMLHttpRequest) {
    return new XMLHttpRequest(); //Not IE
    } else if(window.ActiveXObject) {
    return new ActiveXObject("Microsoft.XMLHTTP"); //IE
    } else {
    alert("Your browser doesn't support the XmlHttpRequest object.");

    }
    }

    var Req = getXmlHttpRequestObject();

    Req.open("GET", "fileName.txt", true);

    Req.onreadystatechange = function() {
    if (Req.readyState == 4) {
    var output = Req.responseText;
    }
    }


    It works fine, except that if a text file exists and is then overwritten, it will still read the old version rather than the current version.

    For example, if I have a text file with the contents 'AAA', which is then saved again with the contents 'BBB', the code fails to refresh and still reads 'AAA'.

    Closing the browser tab and reloading the page solves the problem, but I would like to find a solution which doesn't involve doing this.

  2. #2
    Join Date
    Jan 2007
    Location
    Wisconsin
    Posts
    2,120
    Change this:
    HTML Code:
    Req.open("GET", "fileName.txt", true);
    to this:
    HTML Code:
    var right_now = new Date();
    Req.open("GET", "fileName.txt?" + right_now.getTime(), true);

  3. #3
    Join Date
    Nov 2010
    Posts
    2
    I've changed the code as above, and it's now working perfectly.

    Thanks for your help.

  4. #4
    Join Date
    Mar 2013
    Posts
    3
    why do you make this change. I have also this problem, If I don't add right_now.getTime() for "fileName.txt?", the html page can not be refrehedccorrectly?

    the follow is my codes

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    	<head>
    		<title>Ajax</title>
            <link rel="stylesheet" type="text/css" href="index_styling.css" />
    		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    		<script type="text/javascript">
    
    		    function Ajax() {
    		        var $http, $self = arguments.callee;
    
    		        if (window.XMLHttpRequest) {
    		            $http = new XMLHttpRequest();
    		        } else if (window.ActiveXObject) {
    		            try {
    		                $http = new ActiveXObject('Msxml2.XMLHTTP');
    		            } catch (e) {
    		                $http = new ActiveXObject('Microsoft.XMLHTTP');
    		            }
    		        }
    
    		        if ($http) {
    		            $http.onreadystatechange = function () {
    		                if (/4|^complete$/.test($http.readyState)) {
    
    		                    var JSONObject = JSON.parse($http.responseText);
    
    		                    document.getElementById('T1').innerHTML = "T1: " + JSONObject.T1;
    		                    document.getElementById('T2').innerHTML = "T2: " + JSONObject.T2;
    		                    document.getElementById('T3').innerHTML = "T3: " + JSONObject.T3;
    		                    document.getElementById('T4').innerHTML = "T4: " + JSONObject.T4;
    		                    document.getElementById('T5').innerHTML = "T5: " + JSONObject.T5;
    		                    document.getElementById('T6').innerHTML = "T6: " + JSONObject.T6;
    		                    document.getElementById('T7').innerHTML = "T7: " + JSONObject.T7;
    		                    document.getElementById('T8').innerHTML = "T8: " + JSONObject.T8;
    		                    document.getElementById('T9').innerHTML = "T9: " + JSONObject.T9;
    		                    document.getElementById('T10').innerHTML = "T10: " + JSONObject.T10;
    		                    document.getElementById('T11').innerHTML = "T11: " + JSONObject.T11;
    		                    document.getElementById('T12').innerHTML = "T12: " + JSONObject.T12;
    		                    document.getElementById('T13').innerHTML = "T13: " + JSONObject.T13;
    		                    document.getElementById('T14').innerHTML = "T14: " + JSONObject.T14;
    		                    document.getElementById('T15').innerHTML = "T15: " + JSONObject.T15;
    		                    document.getElementById('T16').innerHTML = "T16: " + JSONObject.T16;
    
    		                    setTimeout(function () { $self(); }, 5000);
    		                }
    		            };
    		            $http.open('GET', 'data.txt' + '?' + new Date().getTime(), true);
    		            $http.send(null);
    		        }
    
    		    }
    
    		</script>
    
    	</head>
    	<body>
    
    		<script type="text/javascript">
    		    setTimeout(function () { Ajax(); }, 5000);
    		</script>
    <div style="background:url(TYP400101_PNG.dat) no-repeat;width:1512px;height:794px">
    		<div id="T1"></div>
            <div id="T2"></div>
            <div id="T3"></div>
            <div id="T4"></div>
            <div id="T5"></div>
            <div id="T6"></div>
            <div id="T7"></div>
            <div id="T8"></div>
            <div id="T9"></div>
            <div id="T10"></div>
            <div id="T11"></div>
            <div id="T12"></div>
            <div id="T13"></div>
            <div id="T14"></div>
            <div id="T15"></div>
            <div id="T16"></div>
    </div>
    	</body>
    </html>

  5. #5
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    you really need to fix the server settings for that page. make sure that last-modified and date are being sent correctly.
    you can also emit an expired tag to prevent the undesired caching.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles