Jump to content

Open a text file using javascript


khadra4ever

Recommended Posts

Hi guys!

 

I have a form with a button and a textarea, when i click on the button it opens a text file and displays its content in the textarea

 

the problem is when i update the file and click the button the changes i made to the file don't show in the textarea, it only shows the old content.

 

here's my code

 

<script type="text/javascript">

var req;

var FileName="http://localhost/file.txt";

 

function OpenFile(url) {

  req = false;

    // branch for native XMLHttpRequest object

    if(window.XMLHttpRequest && !(window.ActiveXObject)) {

      try {

        req = new XMLHttpRequest();

        } catch(e) {

        req = false;

        }

    // branch for IE/Windows ActiveX version

    } else if(window.ActiveXObject) {

          try {

          req = new ActiveXObject("Msxml2.XMLHTTP");

        } catch(e) {

          try {

                req = new ActiveXObject("Microsoft.XMLHTTP");

          } catch(e) {

                req = false;

          }

      }

    }

  if(req) {

      req.onreadystatechange = processReqChange;

      req.open("GET", url, true);;

      req.send("");

  }

}

 

function processReqChange() {

    // only if req shows "loaded"

    if (req.readyState == 4) {

        // only if "OK"

        if (req.status == 200) {

            document.getElementById('txtOutput').value=req.responseText;

        } else {

            alert("There was a problem retrieving the XML data:\n" + req.statusText);

        }

    }

}

</script>

 

<input type="button" id="btnOpen" value="Open File" onClick="OpenFile(FileName)" /><br />

<textarea id='txtOutput' rows='20' cols='100'></textarea>

 

//----------------------------------------------End code--------------------------------------------------------------------------

 

Can anybody tell me if anything wrong in my code

Thanks in advance

Link to comment
https://forums.phpfreaks.com/topic/224305-open-a-text-file-using-javascript/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.