annotate src/site.js @ 13:24668255cede

preprocess urls
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 03 Jul 2022 23:59:41 -0600
parents 0472897e790d
children 94e26bffd4fb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7
0472897e790d add javascript
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1
0472897e790d add javascript
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
2 function ajax(url,postData) {
0472897e790d add javascript
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
3 let request = new XMLHttpRequest();
0472897e790d add javascript
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
4 let method = postData ? 'POST' : 'GET';
0472897e790d add javascript
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
5 request.open( method, url );
0472897e790d add javascript
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
6 if( postData )
0472897e790d add javascript
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
7 request.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' );
0472897e790d add javascript
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
8 request.onload = function() {
0472897e790d add javascript
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
9 if( request.status !== 200 ) {
0472897e790d add javascript
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10 window.console && console.log( 'ajax failed: ' + request.status );
0472897e790d add javascript
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
11 if( request.responseText )
0472897e790d add javascript
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
12 document.write('<pre>'+request.responseText+'</pre>');
0472897e790d add javascript
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
13 return;
0472897e790d add javascript
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
14 }
0472897e790d add javascript
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
15 eval( request.responseText );
0472897e790d add javascript
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16 };
0472897e790d add javascript
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17 request.send(postData);
0472897e790d add javascript
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18 }
0472897e790d add javascript
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19
0472897e790d add javascript
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20 window.onerror = function(msg, url, line, col, error) {
0472897e790d add javascript
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21 if (!url)
0472897e790d add javascript
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22 return;
0472897e790d add javascript
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23 let err = msg;
0472897e790d add javascript
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24 err += '\nurl = ' + url;
0472897e790d add javascript
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25 if (url != window.location)
0472897e790d add javascript
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26 err += '\npage = ' + window.location;
0472897e790d add javascript
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
27 err += '\nline = '+line;
0472897e790d add javascript
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
28 if (col)
0472897e790d add javascript
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
29 err += '\ncolumn = ' + col;
0472897e790d add javascript
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
30 if (error && error.stack)
0472897e790d add javascript
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
31 err += '\nstack = ' + error.stack;
0472897e790d add javascript
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
32 ajax( '/error_log.js', 'err='+encodeURIComponent(err) );
0472897e790d add javascript
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
33 };