|
Re: client-side Javascript HTTP: msg#00060mozilla.devel.dom
Felipe Gasper wrote: Does anyone out there know if there's a way for client-side Javascript to fetch a text file via HTTP and read its contents into a variable? The best I can do so far is creating an IFRAME element (via DOM), which does display the text I want, but from there I can't find a way to read the frame's contents into a veriable. Wrong newsgroup, see netscape.public.mozilla.dom for followup. As for the problem, some browsers by now implement an object with an API mainly meant to exchange XML data with the server via HTTP but you can also use that API to read text (as long as it is UTF-8 encoded that is). function getTextContent (url, contentHandler) { var httpRequest; if (typeof XMLHttpRequest != 'undefined') { httpRequest = new XMLHttpRequest(); } else if (typeof ActiveXObject != 'undefined') { try { httpRequest = new ActiveXObject('Microsoft.XMLHTTP'); } catch (e) { httpRequest = null; } } if (httpRequest) { httpRequest.open('GET', url, true); httpRequest.onreadystatechange = function (evt) { if (httpRequest.readyState == 4) { contentHandler(httpRequest.responseText); } }; httpRequest.send(null); } } getTextContent(location.href, function (text) { alert(text); }) That code should work with IE 5 and later on Windows, Netscape 7, Mozilla, Firefox. Safari 1.2 and Opera 7.60 (currently only available as a preview) also make attempts to implement XMLHttpRequest but so far do not provide the full API and versatility, the above fails to display anything for me here with Opera 7.60 Preview 2. I don't have Safari to test here. For more details see http://www.faqts.com/knowledge_base/view.phtml/aid/17226/fid/616 and http://jibbering.com/2002/4/httprequest.html -- Martin Honnen http://JavaScript.FAQTs.com/ |
|
| <Prev in Thread] | Current Thread | [Next in Thread> |
|---|---|---|
| Previous by Date: | Event Model question.: 00060, Stefan Magdalinski |
|---|---|
| Next by Date: | website hosting: 00060, forall2see_2000@xxxxxxxxx |
| Previous by Thread: | Event Model question.i: 00060, Stefan Magdalinski |
| Next by Thread: | website hosting: 00060, forall2see_2000@xxxxxxxxx |
| Indexes: | [Date] [Thread] [Top] [All Lists] |
| News | FAQ | advertise |