address standardization part one, finally checked in from here
[freeside.git] / httemplate / elements / xmlhttp.html
1 <%doc>
2
3 Example:
4
5   include( '/elements/xmlhttp.html',
6     # required
7     'url'  => $p.'misc/something.html',
8     'subs' => [ 'subroutine' ],
9
10     # optional
11     'method' => 'GET', #defaults to GET, could specify POST
12     'key'    => 'unique', #unique key
13
14   );
15
16 </%doc>
17 <SCRIPT TYPE="text/javascript">
18
19   function rs_init_object() {
20     var A;
21     try {
22       A=new ActiveXObject("Msxml2.XMLHTTP");
23     } catch (e) {
24       try {
25         A=new ActiveXObject("Microsoft.XMLHTTP");
26       } catch (oc) {
27         A=null;
28       }
29     }
30     if(!A && typeof XMLHttpRequest != "undefined")
31       A = new XMLHttpRequest();
32     if (!A)
33       alert("Can't create XMLHttpRequest object");
34     return A;
35
36   }
37 % foreach my $func ( @{$opt{'subs'}} ) { 
38 %
39 %       my $furl = $url;
40 %       $furl =~ s/\"/\\\\\"/; #javascript escape
41 %
42 %  
43
44
45     function <%$key%><%$func%>() {
46         // count args; build URL
47         var url = "<%$furl%>";
48         var a = <%$key%><%$func%>.arguments;
49
50         var args;
51         var len;
52         var content = 'sub=<% uri_escape($func) %>';
53         if ( a && typeof a  == 'object'  && a[0].constructor == Array ) {
54             args = a[0];
55             len = args.length
56         } else {
57             args = a;
58             len = args.length - 1;
59         }
60         for (var i = 0; i < len; i++) 
61             content = content + "&arg=" + escape(args[i]);
62         content = content.replace( /[+]/g, '%2B'); // fix unescaped plus signs 
63
64         if ( '<%$method%>' == 'GET' ) {
65           url = url + content;
66         }
67
68         //alert('<%$method%> ' + url);
69
70         var xmlhttp = rs_init_object();
71         xmlhttp.open("<%$method%>", url, true);
72
73         xmlhttp.onreadystatechange = function() {
74             if (xmlhttp.readyState != 4) 
75                 return;
76
77             if (xmlhttp.status != 200) {
78               alert(xmlhttp.status + " status connecting to " + url);
79             } else {
80               var data = xmlhttp.responseText;
81               //alert('received response: ' + data);
82               a[a.length-1](data);
83               if ( data.indexOf("<b>System error</b>") > -1 ) {
84                 var w;
85                 if ( w = window.open("about:blank") ) {
86                   w.document.write(data);
87                 } else {
88                   // popup blocking?  should use an overlib popup instead 
89                   alert("Error popup disabled; try disabling popup blocking to see");
90                 }
91               }
92             }
93         }
94
95         if ( '<%$method%>' == 'POST' ) {
96
97           xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
98           xmlhttp.send(content);
99
100         } else {
101
102           xmlhttp.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
103           xmlhttp.send(null);
104
105         }
106
107         //rs_debug("x_$func_name url = " + url);
108         //rs_debug("x_$func_name waiting..");
109     }
110 % } 
111
112
113 </SCRIPT>
114 <%init>
115 my ( %opt ) = @_;
116
117 my $url = $opt{'url'};
118 my $method = exists($opt{'method'}) ? $opt{'method'} : 'GET';
119 #my @subs = @{ $opt{'subs'};
120 my $key = exists($opt{'key'}) ? $opt{'key'} : '';
121
122 $url .= ( ($url =~ /\?/) ? '&' : '?' )
123   if $method eq 'GET';
124
125 </%init>