-4

Please assist me as I am new to java scripting. I want to show data from a text file which is in "d:\vikas.txt" path to a text box.If anyone can provide me a complete code for this that will be really helpful.I am using Google chrome browser. In case you need any other info please do let me know.

Thanks

user3332404
  • 161
  • 1
  • 4
  • 13
  • I just tried this var sContent = 'vikas'; document.getElementById('textarea').value = sContent; here textarea is my textarea ID .with this vikas is visible in the box but I don't know how I can read a file and display it's data to textarea.. – user3332404 Mar 10 '14 at 14:21
  • can you at least show some attempt at solving this ? – Bobby5193 Mar 10 '14 at 14:21
  • you can use a simple http request i think and get the content – Morrisda Mar 10 '14 at 14:22
  • 4
    SO is not the place for: "Please do my job" kind of questions. – Tarmo Mar 10 '14 at 14:23

1 Answers1

0

You can use an http request to do it and get the file as a static page.

function exec_req(url)
{
objAJAX = false;
if (window.XMLHttpRequest)
{
    try
    {
        objAJAX = new XMLHttpRequest();
        if (objAJAX.overrideMimeType) objAJAX.overrideMimeType('text/xml');
    }
    catch(e) 
    {
        objAJAX = false;
    }
}
else if (window.ActiveXObject)
{
    try 
    {
        objAJAX = new ActiveXObject("Msxml2.XMLHTTP");
    } 
    catch(e) 
    {
        try 
        {
            objAJAX = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch(e)
        {
            objAJAX = false;
        }
    }
}

if (objAJAX)
{

  objAJAX.onreadystatechange = myCallback;  
}
    objAJAX.open("GET", url, true);
    objAJAX.send();
}
}

 function myCallback() {

   if(objAJAX.readyState == 4 && objAJAX.status == 200) {   

    var myText = objAJAX.responseText
        alert(myText)

    }

    else {alert('error while reading this file');}

     }

just call the function exec_req('bla/your_file _link.txt'), and when the file'll be read the function myCallback will be invoked, and the text you are looking for will be available as a local var in that function. This is async. I am not sure it will work locally if you don't run a localhost (Cross origin requests are only supported for HTTP).

Morrisda
  • 1,380
  • 1
  • 11
  • 25