2010-07-12 2 views
2

자바 스크립트를 사용하여 웹 페이지에서 텍스트 파일이나 HTML 파일을 열고 텍스트 파일의 내용이나 HTML 코드를 텍스트 영역으로로드하는 방법 제발 해달라고 javscript를 사용하여 불가능한 말해,하지만 가능하지만 코드와 내 집에 일주일 오지 않을 알아. 예제 코드는 크게 감사하겠습니다. (난 이미이 질문을했지만 모두가 질문의 다른 부분에 집중하는 것 같았다.)HTML 파일의 텍스트 파일이나 HTML 코드의 내용을 먼저 탐색하여 텍스트 영역에로드하는 방법은 무엇입니까?

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

내가, 포럼이 발견 이름을 기억 해달라고,이 작업 코드 만 텍스트 파일을로드 할 수 있습니다 .... 이제 질문은 htmlcodes를 html 파일에서 가져올 수있는 방법입니다. 유 시간 근래 경우

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

이 코더가 작동 할 수 있지만 NT 나를 위해 노력하고 주장하는 또 하나,이 코드를 실행하시기 바랍니다 ... 여기에 전체 코드를 제공하시기 바랍니다 작동하는지. (코라에 의해 코딩)

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

나는이 작업을 수행 할 수있는 ... 나는 htmlcode가 텍스트 영역에 외부 HTML 파일을 형성로드 할 수 있습니다 자바 스크립트를했다 생각하지 않습니다. 그러나 나는 그 파일을 잃어 버렸습니다. 슬픈 사람은 js 함수 (또는 액티브 x)를 만들 수있는 스크립팅 능력을 가지고 있지 않습니다.

+0

그게 불가능합니다 ... 농담 .. :) – Reigel

+0

잘못된 스크립트를 가지고 있습니다. onclick = "", 인용 부호를 이스케이프 처리하거나 따옴표를 제거해야합니다 ... 초 : " ajax_json_echo "루트 폴더 아래에? 서버 쪽 페이지 여야합니다 ... 크로스 브라우저에 대한 요구 사항이 없으면이 링크는 IE에서만 작동합니다. http://msdn.microsoft.com/en-us/library/ms531417 (v = VS.85) .aspx – Ayyash

+0

고맙습니다. IE에서만 작동해야합니다 (다른 브라우저는 신경 쓰지 않아야합니다.) 그리고 havaj ajax _json_echo 파일을 ..... ..... 그래서 gona 사이트를 지금 확인하고 있습니다. – subanki

답변

3

AJAX를 사용하여 확실히 가능합니다. 동일한 출처 제한이 자신의 도메인에서만 작동한다는 것을 명심해야합니다. 한 가지 방법은 jQuery.get 함께 :

편집 : 잘 작동 위

<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> 
+0

위 코드는 함수 onclick 명령을 사용하여 호출하는 방법입니다. – subanki

+1

HTML 문서를 숨겨진 iframe에로드 한 다음 HTML을 가져올 수도 있습니다. http://www.webmasterworld.com/javascript/3083765.htm 참조 – row1

+0

내 코드가 올바른지 ... plz 내가 잘못된 것을하고 있다면 나를 바로 잡으십시오. – subanki

2

대답을하지만 당신은 사용자 정의 할 수 무슨 일을하는지 이해해야한다 : 더 완벽한 데모 (또한 jsFiddle demo 참조). 다음은 IE8 테스트에서 간단한 솔루션 (위에서 언급 한 솔루션을 사용자 정의), 그것의 하고 (당신이 HTML을 사용하면 src 올바른 속성을 가지고 있음을 알고 당신의 iframe에 렌더링 볼 수 있고, 기억) 잘 작동 :

<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> 
+0

나는 javascript에서 매우 멍청한데, 캔트는 그 sybols 및 문자를 쉽게 이해할 수있다. 나는 이미 다른 게시물에서 원하는 것을 얻었습니다. http : //stackoverflow.com/questions/3235883/html-codes-from-external-html-file-in-to-textarea 마지막으로 해결할 수 있습니까? prob 내가 그 게시물에있다. 다시 한 번 감사드립니다. – subanki

관련 문제