Greg Levine

Navigation

Skip navigation.

Search

Site navigation

Email conversation

FromGreg Levine
ToMe
Subjectaccessing a file on a different domain
Date1 September 2005 00:19
Mark,

Thanks so much for providing this service. I found your page while searching
tirelessly for some answers about some javascript I was struggling with and
so far you have answered quite a few questions just from reading your emails
and checking out a few tutorials. Keep up the good work.

I want to give you a little background on my javascript application so that
you will have a decent understanding of what I am asking. I am attempting to
connect to a js file on another domain so I can pull data in that js and
display it. There are about 40k files which can be used, so I am
constructing a url, including filename, using data grabbed from a query
string. I am able to construct the url with no problems. I originally
started using XMLHttpRequest to open the js files. Running the application
from my desktop I was able to open the js files and grab and display the
data. Once putting the application on my web server, I could no longer run
it, and I got an error on the "http.open('GET,...)" line. I posted questions
on a few forums and found out that I couldn't open files on another domain
using this method. So I did some research and tried to come up with another
solution.

This is when I found your website. I specifically came across your page
entitled "Loading external JavaScipt data after the page has loaded"
(http://www.howtocreate.co.uk/loadingExternalData.html). I read though that
page and found where you wrote, "Note: If you want to submit form data to
construct the external data URL, I have demonstrated how in a previous email
(http://www.howtocreate.co.uk/emails/JDDeutschendorf.html)." I found some
very useful info in the email discussion and then found another email
(http://www.howtocreate.co.uk/emails/NicovanNiekerk.html) which talked about
using ASP to serve the external page as if it came from its own domain. This
sounded like a good possibility to me, but I don't know much about ASP. I
was hoping that you could be more specific about how to do this and tell me
if I need any specific API's installed on my web server. I have begun going
through some ASP tutorials to get a feel for using that language (I am
mostly C++ and bits of Java, very little web programming). I am posting my
code at the bottom of this email, in case it is of any use to you to see
what I have done so far, since its probably better than attaching it or
leading you to a website you don't know.

**If you do attempt to test my code then try using "000000" as the ID on the
query string (i.e., playerfinder.html?id=000000). The example js file has a
4 row, multidimensional array, but that array can be anywhere from 2-4 rows,
not that you need that info for anything, but it might help -- I don't know.
Really, being able to output any data what-so-ever would be nice.at this
point.

Thanks again and I hope to hear back from you.

-- 
Greg Levine
FromMe
ToGreg Levine
SubjectRe: accessing a file on a different domain
Date1 September 2005 16:00
Greg,

> using ASP to serve the external page as if it came from its own domain.
> This sounded like a good possibility to me, but I don't know much about ASP.

Unfortunately, neither do I. I know how to do it in PHP, but not in ASP.
However, you will have to use whatever your hosting service has available.
You might want to try asking on ASP forums.

Anyway, this is also possible without server side scripting, since all you
want to do is use script files. This is fairly easy in fact. Use the iframe
technique I described on
http://www.howtocreate.co.uk/loadingExternalData.html

Now, the page that you load into the iframe can embed the script from the
other domain:
window.frames['dataframe'].window.location.replace('moreData.html?'+escape(url));

then in moreData.html:
<script type="text/javascript">
var scriptURL = unescape(location.search.replace(/^\?/,''));
if( scriptURL.match(/^http:\/\/www.example.com\/etc\//) ) {
document.write('<script type=\"text\/javascript\" src=\"'+
  scriptURL+'\"><\/script>')
}
parent.doSomethingWithTheData( rawBatting );
</script>

Mark 'Tarquin' Wilton-Jones - author of http://www.howtocreate.co.uk/
FromGreg Levine
ToMe
SubjectRe: accessing a file on a different domain
Date1 September 2005 16:06
Mark,

Thanks for being so prompt with your response. To tell you the truth, PHP is
better for me anyways since I have a small amount of experience with it. In
the email conversation I read you had mentioned ASP, so I figured I should
ask about that. PHP already resides on my server, so I can easily work with
it. If you wouldn't mind pointing me in the right direction for using PHP
for my problem I would really appreciate it. I am also going to attempt to
use your iframes method because I feel it always helps to know how to solve
a problem in multiple ways -- and maybe it will end up being the best
solution for my purposes.

Thanks,

Greg Levine
FromMe
ToGreg Levine
SubjectRe: accessing a file on a different domain
Date1 September 2005 16:32
Greg,

> PHP is better for me anyways since I have a small amount of experience
> with it.

Then this is what you need. Set the preg_match to use the correct domain
(that's a security precaution). The php file should be loaded using an
address like this:
url = 'foo.php?scriptURL='+escape(url);

<?php
if( !$scriptURL ) {
  $scriptURL = $_GET ? $_GET['scriptURL'] : $HTTP_GET_VARS['scriptURL'];
}
if( $scriptURL && preg_match("/^https?:\/\/example\.com\//",$scriptURL)
  && ( $scriptRef = @fopen($scriptURL,'rb') ) ) {
    //could have used fpassthru, but IE needs the length header
    $contents = '';
    while( !feof( $scriptRef ) ) {
        $contents .= fread( $scriptRef, 8192 );
    }
    header('Content-Type: text/javascript');
    //IE doesn't work without this one
    header('Content-Length: '.strlen($contents));
    print $contents;
} else {
    header('HTTP/1.1 404 Not Found');
}
?>

Tarquin
This site was created by Mark "Tarquin" Wilton-Jones.
Don't click this link unless you want to be banned from our site.