軽くAjaxまとめ
JavaScriptのXMLHttpRequestはブラウザによって判別が必要。
参考:Using the XML HTTP Request object
var xmlhttp = false; /*@cc_on @*/ /*@if (@_jscript_version >= 5) // JScript gives us Conditional compilation, 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; } } @else xmlhttp=false; @end @*/ if (!xmlhttp && typeof XMLHttpRequest!='undefined') { try { xmlhttp = new XMLHttpRequest(); } catch (e) { xmlhttp = false; } }
で、responseText(responseXML)を利用して、文章を取得する。
参考:Safari - Apple Developer
function loadXMLDoc(url) { if(!xmlhttp) return; xmlhttp.open("GET",url,true); xmlhttp.onreadystatechange = function() { if (xmlhttp.readyState == 4) { if (xmlhttp.status == 200) { getData(); }else { alert("There was a problem retrieving the XML data:\n"+xmlhttp.statusText); } } } xmlhttp.send(null); } function getData() { var rexml = xmlhttp.responseText; }
後は、innerHTMLなりで表示に使用する。
Mozilla系統、つまりXULのXMLHttpRequestは絶対URLをopenしない。セキュリティのためか。
で、対策として、CGIにGETでURLを送ってCGIに取ってこさせる。代理サーバみたいなもの。
参考:トップページ | ASH のホームページ
#!/usr/local/bin/perl use strict; use Socket; use FileHandle; my ($http,$proxy_host,$proxy_port); my ($con_host,$con_port); my ($host,$port,$url,$path,$ip,$sockaddr); my ($arg,$buf); ($arg)=$ENV{'QUERY_STRING'}; $http='1.1'; $proxy_host=''; $proxy_port=0; $host='localhost'; $port=getservbyname('http','tcp'); $path='/'; $arg=~m!(http:)?(//)?([^:/]*)?(:([0-9]+)?)?(/.*)?!; if($3){ $host=$3;} if($5){ $port=$5;} if($6){ $path=$6;} if($proxy_host ne ''){ $con_host=$proxy_host; $con_port=$proxy_port; $url=$arg; } else{ $con_host=$host; $con_port=$port; $url=$path; } $ip=inet_aton($con_host) or die "host($con_host) not found\n"; $sockaddr=pack_sockaddr_in($con_port,$ip); socket(SOCKET,PF_INET,SOCK_STREAM,0) or die "socket error\n"; connect(SOCKET,$sockaddr) or die "connect $con_host $con_port error\n"; autoflush SOCKET(1); if($http eq '1.1'){ print SOCKET "GET $url HTTP/1.1\n"; print SOCKET "Host: $host\n"; print SOCKET "Connection: close\n\n"; } else{ print SOCKET "GET $url HTTP/1.0\n\n";} print "Content-Type: text/html\n\n"; while(chomp($buf=)){ print $buf;} close(SOCKET);
実に面白い。クライアントとサーバの間にAjaxエンジンという仕組みを挟む発想が面白い。目指せGoogle系システム。
流行なので検索すると色々な人が面白い事をしていますね。google:Ajax
ただ、Accessibilityとしては失格になるね。Lynxやw3mを使っている人もいるし。
参考:Home | Web Accessibility Initiative (WAI) | W3C