Is there an easy way to get a remote script and run code when it has
loaded as well as check the content type? $.getScript only works with
those scripts on the same server.

The problem I have is when using Google Maps I get often the sorry
CAPTCHA page (the one you get if too many requests are made) instead
of the JavaScript. This results in the rest of the JavaScript (not
just the map) on the page not working. What I want to do is show that
page (preferably in a Thickbox) when it happens so that the map will
still work. The IP address is a common one (which is probably why the
page is loaded) as it is used on a large WAN (NHS infact). I have
asked on the Google Maps Help Group (July 31), but no one replied
(even after replying to my own post over a week later. So I want to
find out how to work around the problem.

I have also tried having a proxy page at the server using ASP.NET and
even then it doesn't work (only part of the script is loaded) which is
a WebHandler:

<%@ WebHandler Language="C#" Class="GoogleMaps" %>

using System;
using System.Web;
using System.Text;
using System.IO;
using System.Net;

public class GoogleMaps : IHttpHandler
{

    public void ProcessRequest (HttpContext ctx)
        {
                ctx.Response.ContentType = "text/javascript";
                ctx.Response.Write(GetRemoteScript(ctx, 
"http://maps.google.com/maps?
file=api&v=2&key=GoogleMapsAPIKey"));
                ctx.Response.End();
    }

    public bool IsReusable
        {
        get
                {
            return false;
        }
    }

        private string GetRemoteScript(HttpContext ctx, string url)
        {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.Referer = "http://www.mywebsite.nhs.uk";;
                request.ContentType = "text/javascript";
                try
                {
                        // get the response
                        HttpWebResponse response = 
(HttpWebResponse)request.GetResponse();
                        // set the content length
                        int contentLength = (int)response.ContentLength;
                        // get the stream
                        Stream responseStream = response.GetResponseStream();
                        // Pipes the stream to a higher level stream reader 
with the
required encoding format.
                        StreamReader readStream = new 
StreamReader(responseStream,
ctx.Response.ContentEncoding);
                        // create a char array
                        char[] data = new char[contentLength];
                        // load from the stream into the char array
                        readStream.Read(data, 0, contentLength);
                        // return the data as a string
                        return new String(data);
                }
                catch(Exception ex)
                {
                        return string.Empty;
                }
                return string.Empty;
        }
}

Reply via email to