Simon Wistow (deflatermouse) wrote,
Simon Wistow
deflatermouse

all the cool kids are AJAXing - why can't I?

We have some user images on a server (let's call it 'a') at work and I wanted to automatically inline them on another page on another server (which, for demonstration purposes we shall call 'b') if they exist. This, I felt, was a job for this AJAX malarkey that I'd heard so much about.

This

    <html>                                                                              
    <head>                                                                              
    <script type="text/javascript">                                                     
    function picture(name)                                                              
    {                                                                                   
        var xmlhttp=false;                                                              
    /*@cc_on @*/                                                                        
    /*@if (@_jscript_version >= 5)                                                      
        // JScript gives us Conditional compilation                                     
        // so we can cope with old IE versions.                                         
        // and security blocked creation of the objects.                                
        try {                                                                           
            xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");                              
        } catch (e) {                                                                   
            try {                                                                       
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");                       
            } catch (E) {                                                               
                xmlhttp = false;                                                        
            }                                                                           
        }                                                                               
    @end @*/                                                                            
        if (!xmlhttp && typeof XMLHttpRequest!='undefined') {                           
            xmlhttp = new XMLHttpRequest();                                             
        }                                                                               
                                                                                    
        var url     = "http://a/"+name+".jpg";                                
        xmlhttp.open("HEAD", url,true);                                                 
        xmlhttp.onreadystatechange=function() {                                         
            if (xmlhttp.readyState==4) {                                                
                if (xmlhttp.status==200) {                                              
                    document.write("<img src='"+url+"'>");                              
                }                                                                       
            }                                                                           
        }                                                                               
        xmlhttp.send(null);                                                             
    }                                                                                   
    </script>                                                                           
    </head>                                                                             
    <body>                                                                              
                                                                                    
    <div>                                                                               
          <script type="text/javascript">                                                   
            picture('foobar')                                                           
          </script>                                                                         
    </div>                                                                              
                                                                                    
    <div>                                                                               
          <script type="text/javascript">                                                   
              picture('doesnt exist')                                                       
          </script>                                                                         
    </div>                                     
    </body>
    </html>

didn't work, throwing up errors like

    "uncaught exception: Permission denied to call method XMLHTTRequest.open"

To get round the security thing I had to stick a script on b

    #!/usr/bin/perl -w                                                                  
                                                                                    
    use strict;                                                                         
    use CGI;                                                                            
    use LWP::Simple;                                                                    
                                                                                    
    $|++;                                                                               
                                                                                    
    my $q = CGI->new;                                                                   
    my $n = $q->param('url');                                                          
    my $u = "http://a/${n}.jpg";                                              
    if (head($u)) {                                                                     
        print $q->header('text/html');                                              
    } else {                                                                            
        print $q->header('text/html', '404 Not Found');                             
    }                                                                                   
                                                                                    
    print "\n";                                                                         
    exit 0; 

And change the line in the javascript to be

    xmlhttp.open("HEAD", "/proxy_check?url="+url,true); 

And lo! It worked. Huzzah!

Tags: ajax, huzzah, images, javascript, job, malarkey
Subscribe

  • I know what I like and I like what I know

    I actually wrote this about 2 years ago and, for some reason, never posted it but I was having a conversation about it recently and figured I might…

  • All the. Small things.

    Way back, when I was at Yahoo!, there was naturally a lot of discussion about the meteoric rise of Google. At the time we'd dumped Altavista and were…

  • Psychosomatic addict insane

    Specially for burr86 it's time for yet another in the far more than you ever wanted to know series. And today this…

  • Post a new comment

    Error

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

    When you submit the form an invisible reCAPTCHA check will be performed.
    You must follow the Privacy Policy and Google Terms of use.
  • 0 comments