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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.