-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.html
42 lines (41 loc) · 1.28 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>ShrinkURL</title>
</head>
<body>
<h2>ShrinkURL - url shortener</h2>
<form id="shrinkForm" action="/add" method="POST">
<input name="url" type="text">
<input type="submit" value="Shrink">
</form>
<p>
<a id="shrunk" href=""></a>
</p>
</body>
<script>
var shrinkForm = document.getElementById("shrinkForm")
var shrunkUrl = document.getElementById("shrunk")
shrinkForm.addEventListener("submit", function(e) {
e.preventDefault()
var form = e.target
var data = new FormData(form)
var req = new XMLHttpRequest()
req.onreadystatechange = function() {
if (req.readyState == 4) {
if (req.status >= 200 && req.status <= 206) {
var data = JSON.parse(req.responseText)
var link = window.location.protocol + "//" + window.location.host + "/" + data.hash
shrunkUrl.setAttribute('href', link)
shrunkUrl.innerText = link
} else {
alert("shrinking failed: " + req.responseText)
}
}
}
req.open(form.method, form.action)
req.send(data)
})
</script>
</html>