0

I have a code to load text-file when input value change:

function ch_name_registration_load_onchange()
{
for(var i=1;i<11;)
                {
                    for(var j=2;j<11;)
                    {
                        temp='ch_name_registration_'+i+j;
                        el = document.getElementById(temp);

                        el.innerHTML='';

                        j++;
                    }
                    i++;
                }
var page;
switch(ch_name_registration_value)
    {
    case 'A':
    page=1;
    break;
    case 'B':
    page=2;
    break;
    case 'C':
    page=3;
    break;
    case 'D':
    page=4;
    break;
    case 'E':
    page=5;
    break;
    case 'F':
    page=6;
    break;
    case 'G':
    page=7;
    break;
    case 'H':
    page=8;
    break;
    case 'I':
    page=9;
    break;
    }
    var count=0;
    count = parseInt(count,10);
    var xmlhttp;
    var temp;
    if (window.XMLHttpRequest)
        {// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp=new XMLHttpRequest();
        }
    else
        {// code for IE6, IE5
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
    xmlhttp.onreadystatechange=function()
    {
    var el
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            var arr = [];
            arr = xmlhttp.responseText.split("\t");
            for(var i=1;i<11;)
                {
                    for(var j=2;j<11;)
                    {
                        temp='ch_name_registration_'+i+j;
                        el = document.getElementById(temp);


                        el.innerHTML = arr[count]; //display output in DOM
                        if(el.innerHTML =='undefined')
                        el.innerHTML='';
                        count=count+1;
                        j++;
                    }
                    i++;
                }


        }
    }
    xmlhttp.open("GET","CHNAME"+page+".txt",true);
    xmlhttp.send();
}

When I change content of 1 file by notepad and reload it in browser, sometimes it work correctly, sometimes it load old file. I don't know why it load old file instead of current file.

user3054044
  • 29
  • 1
  • 5

1 Answers1

0

This is certainly a caching issue.

If you want to force the browser to always reload the txt file, a common technique is to generate a unique URL. For example:

xmlhttp.open("GET","CHNAME"+page+".txt?_ts="+new Date().getTime(),true);
Christophe
  • 24,147
  • 23
  • 84
  • 130