tags:

views:

42

answers:

3

Hi,

I'm trying to get at a text file from an external website, for use with scripts running from my own domain. Example:

// run from www.mysite.com:
<html>
  <head>
    <script>
      function blah() {
        var data = document.getElementById("thedata");
        alert(data.innerHtml);
      }
    </script>
  </body>
  <body>
    <embed id="thedata" src="http://someotherwebsite.com/data.txt" HEIGHT=60 WIDTH=144>
  </body>
</html>

This is not the best example, but basically I'd like to use javascript to get the contents imported from that external text file, and then do some computations on it.

I'm almost 100% sure this is not allowed, because of all the malicious things you can do with accessing data from other sites and all that stuff. Just wanted to see if there was some legit way of doing this,

Thanks

+1  A: 

AFAIK, there are some ways of letting cross-domain ajax happen, but I've always just made a PHP page on the local domain that loads and prints the external page, which then can be accessed by javascript without a problem.

monksp
True I could do that, but then I'm burdened with all the extra bandwidth right? Like if my users want to grab documents from wikipedia to manipulate, then I am serving all that content through my domain instead of letting their browsers do that work by going directly to the source?
Actually, Wikipedia has a API (http://www.mediawiki.org/wiki/API) and it supports JSONP, so you /can/ use it cross-domain.
Matthew Flaschen
Yeah, no way around that, unfortunately.
monksp
Wikipedia's api will return a JSON response, but doesn't it still require access via normal means? Ajax calls die when trying to go out, not depending on response type, correct?
monksp
Matthew Flaschen
Ok cool yeah JSONP is what I need then - I don't get it though, can't people just exploit JSONP to do malicious stuff? Thanks.
JSONP requires the server cooperate with you, so it won't work on all sites (and the exploit risk is limited).
Matthew Flaschen
I'd never heard of that before. Pretty nifty. Thanks for the link.
monksp
A: 

You are correct. As always, there are various arcane exceptions. the only one I can think of in this case is if data.txt parses as JavaScript.

Matthew Flaschen
A: 

I haven't tested this in every browser, (*), but I'm pretty sure you can just load it in an invisible iFrame and address the content from there ...

<!DOCTYPE html>
<html>
<head></head>
<body>
<iframe name="myiframe" src="http://localhost/~ben/test_text.txt"&gt;&lt;/iframe&gt;
</body>
</html>

And then use Javascript to address the iFrame object, (I've used a quick Jquery version here) ...

<script>
alert(window.myiframe.document.body.innerHTML);
</script>

(*) This reference suggests its possible in most browsers, including IE ...
http://roneiv.wordpress.com/2008/01/18/get-the-content-of-an-iframe-in-javascript-crossbrowser-solution-for-both-ie-and-firefox/

beseku
This is cross-browser...but only for same-domain. It will /not/ work cross-domain.
Matthew Flaschen