Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
127
rated 0 times [  129] [ 2]  / answers: 1 / hits: 18517  / 14 Years ago, mon, july 12, 2010, 12:00:00

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(/[rn]/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.


More From » html

 Answers
6

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></script>
<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>

[#96267] Thursday, July 8, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
elliem

Total Points: 415
Total Questions: 117
Total Answers: 94

Location: American Samoa
Member since Fri, Aug 26, 2022
2 Years ago
;