How to get remote file content using an iframe

  • Thread starter gimme_this_gimme_that
  • Start date
G

gimme_this_gimme_that

In the snippet of code below the url http://someurl.com/data.txt
points to a csv file having a txt suffix.

I want to load the csv file and parse it in JavaScript.

In this example t.innerHTML is defined but has no content.

What can be done to get the content (the content of the txt file)?

Why isn't t.firstChild.nodeValue defined? Can the content be fetched
from the DOM?

Thanks.

<iframe id="data" src="http://someurl.com/data.txt"
style="visibility:hidden"></iframe>
<script language="JavaScript">
var t = document.getElementById('data');
alert(t.innerHTML);
alert(t.firstChild.nodeValue);
</script>
 
T

Tom de Neef

In the snippet of code below the url http://someurl.com/data.txt
points to a csv file having a txt suffix.

I want to load the csv file and parse it in JavaScript.

In this example t.innerHTML is defined but has no content.

What can be done to get the content (the content of the txt file)?

Why isn't t.firstChild.nodeValue defined? Can the content be fetched
from the DOM?

Thanks.

<iframe id="data" src="http://someurl.com/data.txt"
style="visibility:hidden"></iframe>
<script language="JavaScript">
var t = document.getElementById('data');
alert(t.innerHTML);
alert(t.firstChild.nodeValue);
</script>

That would be an interesting way of loading external content into
javascript.
The problem is that <iframe>'s contents is not part of its innerHTML.
Try <iframe ...>test</iframe> and see what the alert returns. It will show
'test', because that is all there is between the opening and closing tags.
Tom
 
P

pr

In the snippet of code below the url http://someurl.com/data.txt
points to a csv file having a txt suffix.

I want to load the csv file and parse it in JavaScript.

In this example t.innerHTML is defined but has no content.

What can be done to get the content (the content of the txt file)?

Why isn't t.firstChild.nodeValue defined? Can the content be fetched
from the DOM?

Thanks.

<iframe id="data" src="http://someurl.com/data.txt"
style="visibility:hidden"></iframe>
<script language="JavaScript">
var t = document.getElementById('data');
alert(t.innerHTML);
alert(t.firstChild.nodeValue);
</script>

document.getElementById("data").contentWindow.document.body.innerHTML

presuming the data is served from the same domain. Why a hidden iframe
rather than an XMLHttpRequest?
 
G

gimme_this_gimme_that

Thanks Pr.

This doesn't do it.

I'm using IE 6.

This is defined:

document.getElementById("data").contentWindow.document

This is null:

document.getElementById("data").contentWindow.document.body

This is undefined:

document.getElementById("data").contentWindow.document.firstNode

What else might I try?

I wrapped the csv content in <html><body></body></html> tags and
renamed the file as an html file. I'd prefer to keep it a csv/text
file.


Why not use HTMLRequest? Because I'm not loading XML? Because there is
no onclick event.

What I'm doing is loading a csv file. From there I want to parse the
content and do some stuff with it in JavaScript.
 
P

pr

Thanks Pr.

This doesn't do it.

I'm using IE 6.

This is defined:

document.getElementById("data").contentWindow.document

This is null:

document.getElementById("data").contentWindow.document.body

It works for me on a sample .txt file in both IE6 and Firefox.
This is undefined:

document.getElementById("data").contentWindow.document.firstNode

It's firstChild. But if document.body was null, something's amiss.
What else might I try?

I wrapped the csv content in <html><body></body></html> tags and
renamed the file as an html file. I'd prefer to keep it a csv/text
file.

That's why I asked about XMLHttpRequest.
Why not use HTMLRequest? Because I'm not loading XML? Because there is
no onclick event.

It isn't just for XML. I don't know what you mean about onclick. A text
file in a hidden iframe doesn't have an 'onclick event'.
What I'm doing is loading a csv file. From there I want to parse the
content and do some stuff with it in JavaScript.

Use XMLHttpRequest. See <URL:
http://www.jibbering.com/2002/4/httprequest.html>
 
T

Tom de Neef

Thanks Pr.

This doesn't do it.

I'm using IE 6.

This is defined:

document.getElementById("data").contentWindow.document

This is null:

document.getElementById("data").contentWindow.document.body

This is undefined:

document.getElementById("data").contentWindow.document.firstNode

What else might I try?

I wrapped the csv content in <html><body></body></html> tags and
renamed the file as an html file. I'd prefer to keep it a csv/text
file.


Why not use HTMLRequest? Because I'm not loading XML? Because there is
no onclick event.

What I'm doing is loading a csv file. From there I want to parse the
content and do some stuff with it in JavaScript.

The XMLHttpRequest works ! Great solution; I'm using it now for loading a
txt file into array opg:

function loadFile(url)
{
var req = false;
// branch for native XMLHttpRequest object
if (window.XMLHttpRequest && !(window.ActiveXObject))
{
try { req = new XMLHttpRequest() }
catch(e) { req = false }
}
else // branch for IE/Windows ActiveX version
{
if (window.ActiveXObject)
{
try { req = new ActiveXObject("Msxml2.XMLHTTP") }
catch(e) { try { req = new ActiveXObject("Microsoft.XMLHTTP") }
catch(e) { req = false }
}
}
}

if(req)
{
req.open("GET", url, false);
req.send("");
return req.responseText
}
return ''
}

var opg =
loadFile("ww.opg").split(String.fromCharCode(13)+String.fromCharCode(10))

Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top