views:

64

answers:

2

Using javascript, how to open a text-file or html-file from a web-page and load the contents in the text file or the html code to a textarea. Please Dont tell me this not possible using javsscript, it is possible but i dont know the code and my bro not coming home for a week . Example codes will be greatly appreciated. (i have already asked this question but everyone seemed to concentrate on the other part of the question.)

<html>
<body>
<form name="abc">
<textarea name="text">loaded text here</textarea>
<input type="button" onClick=""> open file
</form>
</body>
</html>

I found this in a forum , dont remember name , This is a working code but can only load text files .... now the question is how can I get it to fetch htmlcodes from a html file.

<html>
<head>

<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">


<script type="text/javascript">

var ForReading = 1, ForWriting = 2, ForAppending = 8;
var objFSO = new ActiveXObject("Scripting.FileSystemObject");

function loadFile(btn, obj, div, fld) {
objFile = objFSO.GetFile(obj.value);
objStream = objFile.OpenAsTextStream(ForReading);
fld.value = objStream.ReadAll();
objStream.Close();
objStream= null;
fileSpecs(div, btn);
objFile = null;
return true;
}

</script>
</head>

<body>
<form name="myForm" onsubmit="return false;">

<textarea rows="25" name="fileArea" cols="70"
onkeypress="return checkText(this, btnSave);"></textarea> </td>


<input type="file" name="fileName" size="50"
onchange="return checkFile(this, document.getElementById('fileSpec'), btnLoad, btnSave, fileArea);">

<input type="button" name="btnLoad" value="Load" 
onclick="return loadFile(this, fileName, document.getElementById('fileSpec'), fileArea);"> 

</form>
</body>
</html>

This is another one the coder claims it to be working but it is nt working for me , please execute this code if u hav time ...if it is working please provide the complete code here. (coded by Ancora)

<html>
<head>
<script type="text/javascript">

function init(){

var extText = window.frames.messageTxt.document.body.lastChild.lastChild.data;
extText = extText.replace(/[\r\n]/g," ");
document.forms[0].nMessage.value = extText;
}

window.onload=init;

</script>
</head>
<body>
<iframe name='messageTxt' src='txtData.txt'  style='display:none'></iframe>
<form>
<textarea name='nMessage'></textarea>
<input type="button" value="click" onClick="init()">
</form>
</body>
</html>

i guess no can do this...i had a javascript that can load htmlcode form an external html file into the textarea. But i lost that file, sad no one here has the scripting abilities to create a js function (or active x) that can do the same.

+2  A: 

It's definitely possible, using AJAX. You have to keep in mind that same-origin restrictions mean it will only work on your own domain. One way is with jQuery.get:

EDIT: A more complete demo (see also jsFiddle demo):

<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.js"&gt;&lt;/script&gt;
<script type="text/javascript">

    function download_to_textbox(url, el)
    {
      $.get(url, null, function(data)
                       {
                         el.val(data);
                       }, "text");
    }

    $(function()
    {
      $('#open').click(function()
      {
        download_to_textbox("/ajax_json_echo/?foo=bar", $("textarea[name='text']"));
      });
    });
</script>
</head>
<body>
    <form name="abc">
        <textarea name="text" rows="20" cols="70">loaded text here</textarea>
        <input id="open" value="open" type="button">
    </form>
</body>
</html>
Matthew Flaschen
the above code is that a function how do i call it using onclick command
subanki
You can also load the HTML document into a hidden iframe and then get the HTML from there. See http://www.webmasterworld.com/javascript/3083765.htm
row1
is my code correct ...plz rectify me if i am doing anything wrong
subanki
@subanki, if you have an inline event listener, you need to be sure you balance the quotes correctly. It's cleaner to use a separate listener. Also remember that the page must exist on the domain you're testing with. Try this [jsFiddle demo](http://jsfiddle.net/LApCF/2/).
Matthew Flaschen
in that page y am i getting this as result{"post_response": {}, "get_response": {"foo": "bar"}}
subanki
Thanx (A big one) i greatly appreciate your effort but by domain did u mean client computer .. the computer in which the script is being executed ... if yes then it still doesn't open up the "open dialog box"
subanki
@subanki, it's not supposed to open a dialog box. Same domain/origin means roughly same webserver. See [same origin policy](http://en.wikipedia.org/wiki/Same_origin_policy). That is the output expected for the demo. It is being echoed by the server.
Matthew Flaschen
A: 

Hmm, you want an easy straight forward answer dont you? how much do you know of Javascript or HTML? the answers above, all of them work fine but you have to understand what theyre doing to customize... here is the simplest solution (customizing a solution stated above) Tested on IE8 and works fine (remember, you have to see the html render in your iframe to know that you got the "src" attribute correct

<html> 
<head> 
<script type="text/javascript">   
function init(){ 
    var extText = window.frames[0].document.getElementsByTagName("HTML")[0].outerHTML;
    document.getElementById("nMessage").innerText = extText;
} 

</script> 
</head> 
<body> 
    <iframe name="messageTxt" src="[place the path to your html file here]" ></iframe> 
    <form> 
        <textarea name="nMessage" id="nMessage" cols="100" rows="20"></textarea> 
        <input type="button" value="click" onClick="init()" /> 
    </form> 
</body> 
</html>
Ayyash
i am very noob at javascript, cant understand those sybols and characters easily, anyways thanx for the effort u took. i have already got what i wanted in an another post...http://stackoverflow.com/questions/3235883/html-codes-from-external-html-file-in-to-textarea can u solve the last prob i have in that post . thanks again
subanki