Mercurial Hosting > chat
comparison src/site.js @ 3:2c63b10781e1
add login
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 24 Oct 2024 21:43:44 -0600 |
parents | |
children | dade6a560494 |
comparison
equal
deleted
inserted
replaced
2:ee1f91e67509 | 3:2c63b10781e1 |
---|---|
1 'use strict'; | |
2 | |
3 function ajax(url,postData,context) { | |
4 let request = new XMLHttpRequest(); | |
5 let method = postData ? 'POST' : 'GET'; | |
6 request.open( method, url ); | |
7 if( postData ) | |
8 request.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' ); | |
9 request.onload = function() { | |
10 if( request.status !== 200 ) { | |
11 let err = 'ajax failed: ' + request.status; | |
12 if( request.responseText ) { | |
13 err += '\n' + request.responseText.trim(); | |
14 document.write('<pre>'+request.responseText+'</pre>'); | |
15 } | |
16 err += '\nurl = ' + url; | |
17 err += '\npage = ' + window.location; | |
18 ajax( '/error_log.js', 'err='+encodeURIComponent(err) ); | |
19 return; | |
20 } | |
21 try { | |
22 eval( request.responseText ); | |
23 } catch(e) { | |
24 console.log( request.responseText ); | |
25 window.err = '\najax-url = ' + url; | |
26 throw e; | |
27 } | |
28 }; | |
29 request.send(postData); | |
30 } | |
31 | |
32 window.onerror = function(msg, url, line, col, error) { | |
33 if( !url ) | |
34 return; | |
35 let err = msg; | |
36 err += '\nurl = ' + url; | |
37 if( url != window.location ) | |
38 err += '\npage = ' + window.location; | |
39 err += '\nline = '+line; | |
40 if( col ) | |
41 err += '\ncolumn = ' + col; | |
42 if( error ) { | |
43 if( error.stack ) | |
44 err += '\nstack = ' + error.stack; | |
45 if( error.cause ) | |
46 err += '\ncause= ' + error.cause; | |
47 if( error.fileName ) | |
48 err += '\nfileName= ' + error.fileName; | |
49 } | |
50 if( window.err ) { | |
51 err += window.err; | |
52 window.err = null; | |
53 } | |
54 ajax( '/error_log.js', 'err='+encodeURIComponent(err) ); | |
55 }; | |
56 | |
57 function ajaxForm(url,form) { | |
58 let post = ''; | |
59 for( let i=0; i<form.length; i++ ) { | |
60 let input = form[i]; | |
61 let name = input.name; | |
62 if( name === '' ) | |
63 continue; | |
64 let type = input.type; | |
65 if( (type==='radio' || type==='checkbox') && !input.checked ) | |
66 continue; | |
67 post += name + '=' + encodeURIComponent(input.value) + '&'; | |
68 } | |
69 ajax(url,post,{form:form}); | |
70 } | |
71 | |
72 function logout() { | |
73 document.cookie = 'user=; Max-Age=0; path=/;'; | |
74 document.cookie = 'password=; Max-Age=0; path=/;'; | |
75 location = '/'; | |
76 } |