简体   繁体   中英

read txt file with javascript

I am using this code for reading files. Everything is OK with Chrome and FF, but IE does not update the data from the file ... seems to be reading from cache??? Any suggestions? Thanks

if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","YOUR_FILE.txt",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseText;

You could try adding a dynamic string argument to the end of the filename to prevent caching.

Try this:

var time = new Date().getTime();
xmlhttp.open("GET", "YOUR_FILE.txt?time=" + time, false);

Check out Javascript's Date.getTime() documentation.

Please see http://msdn.microsoft.com/en-us/library/ms537505(v=vs.85).aspx
Microsoft suggests slightly different code:

var xmlHttp = null;
if (window.XMLHttpRequest) {
  // If IE7, Mozilla, Safari, and so on: Use native object.
  xmlHttp = new XMLHttpRequest();
}
else
{
  if (window.ActiveXObject) {
     // ...otherwise, use the ActiveX control for IE5.x and IE6.
     xmlHttp = new ActiveXObject('MSXML2.XMLHTTP.3.0');
  }
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM