eliminate the '0 status connecting' errors, they're not telling us anything and causi...
[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               if ( xmlhttp.status != 0 ) {
79                 //not warning on the 0 errors, they pop up when navagating away
80                 // from the page
81                 alert(xmlhttp.status + " status connecting to " + url);
82               }
83             } else {
84               var data = xmlhttp.responseText;
85               //alert('received response: ' + data);
86               a[a.length-1](data);
87               if ( data.indexOf("<b>System error</b>") > -1 ) {
88                 var w;
89                 if ( w = window.open("about:blank") ) {
90                   w.document.write(data);
91                 } else {
92                   // popup blocking?  should use an overlib popup instead 
93                   alert("Error popup disabled; try disabling popup blocking to see");
94                 }
95               }
96             }
97         }
98
99         if ( '<%$method%>' == 'POST' ) {
100
101           xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
102           xmlhttp.send(content);
103
104         } else {
105
106           xmlhttp.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
107           xmlhttp.send(null);
108
109         }
110
111         //rs_debug("x_$func_name url = " + url);
112         //rs_debug("x_$func_name waiting..");
113     }
114 % } 
115
116
117 </SCRIPT>
118 <%init>
119 my ( %opt ) = @_;
120
121 my $url = $opt{'url'};
122 my $method = exists($opt{'method'}) ? $opt{'method'} : 'GET';
123 #my @subs = @{ $opt{'subs'};
124 my $key = exists($opt{'key'}) ? $opt{'key'} : '';
125
126 $url .= ( ($url =~ /\?/) ? '&' : '?' )
127   if $method eq 'GET';
128
129 </%init>