Skip to content
This repository was archived by the owner on Mar 21, 2025. It is now read-only.

Commit b4f5b34

Browse files
committed
Added Type script examples.
1 parent d49a632 commit b4f5b34

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

TypeScript/current_year.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const year = <HTMLElement>document.getElementById('year'); //get div you want to put year in
2+
year.innerHTML = new Date().getFullYear().toString(); //make html of div be the current year

TypeScript/form_sumit_post_ajax.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//function AJAX request
2+
function AjaxRequest(search: string | null | undefined, uri: string) { //in server api, search can be empty
3+
4+
//typical ajax call, used this guide to change from jquery: http://youmightnotneedjquery.com/#request
5+
const request = new XMLHttpRequest();
6+
request.open('POST', uri, true); //post request
7+
request.onload = function () {
8+
if (this.status >= 200 && this.status < 400) { //on success
9+
var resp = this.response;
10+
console.log(resp);
11+
} else { //on fail
12+
console.log('Error on the server. Check you have the correct url.');
13+
}
14+
};
15+
request.onerror = function () { //other errors
16+
console.log('Not able to connect to server');
17+
};
18+
request.send(search);
19+
}
20+
21+
const search_form = <HTMLElement>document.getElementById('search-form'); //get search form by its id
22+
search_form.addEventListener('submit', function (event) { //when form submitted
23+
event.preventDefault();
24+
let search: string;
25+
search = (document.getElementById("search") as HTMLInputElement).value; //get search text field value
26+
AjaxRequest(search, '/api/search/' + search); //send request to API function
27+
})

0 commit comments

Comments
 (0)