-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
34 lines (29 loc) · 1.06 KB
/
script.js
File metadata and controls
34 lines (29 loc) · 1.06 KB
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
document.addEventListener('DOMContentLoaded', function () {
const urlForm = document.getElementById('urlForm');
const resultContainer = document.getElementById('resultContainer');
const shortUrlElement = document.getElementById('shortUrl');
urlForm.addEventListener('submit', function (event) {
event.preventDefault();
const longUrlInput = document.getElementById('longUrl');
const longUrl = longUrlInput.value.trim();
// Make an AJAX request to your backend
fetch('/api/url/shorten', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ longUrl }),
})
.then(response => response.json())
.then(data => {
// Assuming the backend returns the shortened URL
const shortUrl = data.shortUrl;
resultContainer.style.display = 'block';
shortUrlElement.textContent = shortUrl;
})
.catch(error => {
console.error(error);
// Handle error
});
});
});