Reputation: 1429
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.
Upvotes: 2
Views: 18512
Reputation: 4399
The answers above work fine but you have to understand what they're doing to customize.
Here is the simplest solution (customizing a solution stated above), it's
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>
Upvotes: 2
Reputation: 284816
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>
Upvotes: 3